Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When creating an exception class should I implement all constructors of java.lang.Exception?

Tags:

java

exception

When creating an exception class is it okay to only provide a "specific" constructor:

public CircularLinkException(final String msg, final String link)
{
    super(msg);
    link = inputName;
}

or should I implement the constructors of Exception:

public class CircularLinkException extends Exception
{
    public CircularLinkException() { /* ... */ }
    public CircularLinkException(final String msg) { /* ... */ }
    public CircularLinkException(final Throwable t) { /* ... */ }
    public CircularLinkException(final String msg, final Throwable t) { /* ... */ }
}
like image 729
Micha Wiedenmann Avatar asked Jan 14 '13 10:01

Micha Wiedenmann


People also ask

Should Java constructors throw exceptions?

The short answer to the question “can a constructor throw an exception in Java” is yes! Of course, properly implementing exceptions in your constructors is essential to getting the best results and optimizing your code.

How many constructor does the class exception have?

The class Exception contains two constructors.

Which of the following is true when creating own exception class?

Always create your own exception classes. Like any other class, an exception class can contain fields and methods. The new exception class should extend RuntimeException if the program should be required to handle the exception.

What happens if a constructor throws an exception?

When throwing an exception in a constructor, the memory for the object itself has already been allocated by the time the constructor is called. So, the compiler will automatically deallocate the memory occupied by the object after the exception is thrown.


2 Answers

You should create all the Constructors you will use.

Don't add code when you imagine a use for something. Add it when you have a requirement for it.

http://c2.com/xp/YouArentGonnaNeedIt.html

like image 157
Peter Lawrey Avatar answered Oct 11 '22 04:10

Peter Lawrey


It is not actually required to implement any particular constructor. One arbitrary can be implemented in order to construct the exception at all.

Usually two cases happen:

  • Your code realizes that something went wrong after checking something. It wants to raise exception. For such case, a constructor that takes a string message is required. It is usually good to explain in the message that went wrong.
  • Your code tries to do something and catches exception that cannot be thrown further as it is not declared. Then you still can explain that went wrong in the message, put some important context data there, but also it is vital to provide that catched exception for diagnostic. For such case, you need a constructor that takes String and Throwable.

I think, the version without parameters and the version that takes Throwable only are more for lazy people and you just enforce more order in your code by not implementing these.

If your specific exception benefit from some additional information that is always available, surely the constructor should have additional parameters, forcing to pass this information. In your case, a version with the two string parameters should be preserved but probably a version that also takes Throwable as a third parameter would be good. Many known exceptions (like IOException, for instance) went through the pain from "we will never need this kind of constructor" towards adding such constructor in the later versions.

like image 36
Audrius Meškauskas Avatar answered Oct 11 '22 06:10

Audrius Meškauskas