I just want to know why we call super in own created custom exception.
public class MyException extends Exception
{
public MyException(String message)
{
super(message);
}
}
Here What is the use of calling super(message)
The use of super is to call the constructor of the super(base, parent) class which happens to be the Exception
class
Since a derived class always has the base class as a template, it is necessary to initialize the base class as the first step in constructing the derived object. By default, if no super
call is made, Java will use a default (parameterless) constructor to create the base class. If you want a different constructor to be used, you have to use super
to pass in the parameters you want and invoke the correct constructor.
In the case of custom exceptions, it is common to use super
to initialize the exception's error message; by passing the message into the base class constructor, the base class will take care of the work of setting the message up correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With