Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Super call in custom exception

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)

like image 549
Java_Alert Avatar asked Dec 17 '12 04:12

Java_Alert


2 Answers

The use of super is to call the constructor of the super(base, parent) class which happens to be the Exception class

like image 70
codeMan Avatar answered Oct 02 '22 14:10

codeMan


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.

like image 34
Platinum Azure Avatar answered Oct 02 '22 16:10

Platinum Azure