Posts

Showing posts with the label user defined exceptions in java with example

user defined exceptions in java with example

The built-in exceptions in java are not able to describe a certain situation. In such cases, like the built-in exceptions, the user(programmer) can also create his own exceptions which are called 'User-defined exceptions'. The following steps are followed in creation of user-defined exceptions. The user should create an exception class as a subclass to Exception class. Since all exceptions are subclasses of Exception class, the user should also make his class a subclass to it. 1 class MyException extends Exception The user can write a default constructor in his own exception class. He can use it in case he does not want to store any exceptions details. If the user does not want to create an empty object to his exception class, he can eliminate writing the default constructor. 1 MyException () {} The user can create a parametrised constructor with a string as a parameter. He can use this to store exception details. He can call super class (Exc