Micha Wiedenmann
Micha Wiedenmann

Reputation: 20843

When creating an exception class should I implement all constructors of java.lang.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) { /* ... */ }
}

Upvotes: 5

Views: 1526

Answers (6)

Yogesh Ralebhat
Yogesh Ralebhat

Reputation: 1466

It is ok to provide specific constructor and more constructors can be added as and when required. Personally I don't go for all the constructors from super class.

But it all depends on how the new Exception class "CircularLinkException" is going to be used. If this exception class is a part of some exception library which will be used by other users, then, may be, it is a good idea to provide "constructors from Exception class" + "new constructors". This way user has a option to choose between constructors depending on his needs.

If you are the only user of this exception class, then it is better to analyze which constructors are useful for your code and write those only. If you think that some constructors can be useful in future, you can always write them in the future :).

Upvotes: 0

Jatin Khurana
Jatin Khurana

Reputation: 1175

It's not necessary to have all the constructor in your class. Create the constructor according to your need but keep in mind that Exception class in java does not have default constructor so always call the parent class constructor from the first line of the child class while making your own exception (Custom Exception).

Upvotes: 0

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

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.

Upvotes: 1

Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

As I understand you want to make your own exception

public class CircularLinkException extends Exception

so you can have arbitrary number of constructors for CircularLinkException but each of them should call super(smthg), where it is one out of 4 from here

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533500

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

Upvotes: 10

Archer
Archer

Reputation: 5147

You can declare only specific constructor.

Upvotes: 0

Related Questions