Java – Super call in custom exception

I just want to know what we call super. in the custom exception we created.

public class MyException extends Exception 
{ 
   public MyException(String message)         
  {  
    super(message);        
  }      
}

What's the use of calling super here?

Solution

Because derived classes always use the base class as a template, you must initialize the base class as the first step in constructing derived objects By default, Java uses the default (parameterless) constructor to create the base class if no super call is made If you want to use a different constructor, you must use super to pass the required parameters and call the correct constructor

In the case of custom exceptions, super is usually used to initialize the error message of the exception; By passing the message to the base class constructor, the base class will be responsible for setting the message correctly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>