User Defined Exception in Java

In Java programming, it is possible to create our own exception and such exception are called user defined exception.

Exception class is a predifine class. Any user defined exception created by us must be subclass of predefine Exception class and created by extending Exception class.

 The throw and throws keyword are used for implementing user defined exception.                     

Example1:   write a program to take a number from user by using command line argument during run time. if user will enter  more than 100 then grnerate user defined exception MyException.

Explaination: In the above program, name of user define exception is NumberIsGreaterException. 

In the above program user enter  the number 111 during run time using command line argument.

Here value of “m” is greter than 100 therefore  “throw  newNumberIsGreaterException ();” this throw statement throw the exception and catch by catch block, but before thorw “NumberIsGreaterException ()” constructer will call.