You can also create your own exception sub class simply by extending java Exception class or RuntimeException class. You can define a constructor for your Exception sub class (not compulsory) and you can override the toString() function to display your customized message on catch.
classMyExceptionextendsException{privateintex;MyException(inta){ex=a;}publicStringtoString(){return"MyException["+ex+"] is less than zero";}}classTest{staticvoidsum(inta,intb)throwsMyException{if(a<0){thrownewMyException(a);}else{System.out.println(a+b);}}publicstaticvoidmain(String[]args){try{sum(-10,10);}catch(MyExceptionme){System.out.println(me);}}}
The try block contains a block of program statements within which an exception might occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block.
The corresponding catch block executes if an exception of a particular type occurs within the try block.
A try block must followed by a catch block or finally block or both.
Syntax of try-catch in Java
try{//statements that may cause an exception}catch(exception(type)e(object)){//error handling code}
A try block can be followed by multiple catch blocks.
You can add any number of catch blocks after a single try block. If an exception occurs in the guarded code, the exception is passed to the first catch block in the list.
If the exception type of exception, matches with the first catch block it gets caught. If not, the exception is passed down to the next catch block.
This continue until the exception is caught or falls through all catches (will shows a system generated message)
While using multiple catch statements, it is important to remember that: exception subclasses inside catch must come before any of their super classes, otherwise it will lead to compile time error.
A finally statement must be associated with a try statement. It identifies a block of statements that needs to be executed regardless of whether or not an exception occurs within the try block.
In normal execution the finally block is executed after try block. When any exception occurs first the catch block is executed and then finally block is executed.
An exception in the finally block, exactly behaves like any other exception.
The code present in the finally block executes even if the try or catch block contains control transfer statements like return, break or continue.
Syntax of finally block
try{//statements that may cause an exception}finally{//statements to be executed}
If you do not explicitly use the try catch blocks in your program, Java will provide a default exception handler, which will print the exception details on the terminal, whenever exception occurs.
Super class Throwable overrides toString() function, to display error message in form of string.
While using multiple catch block, always make sure that exception subclasses come before any of their super class. Else you will get compile time error (unreachable …)
In nested try catch, the inner try block, uses its own catch block as well as catch block of the outer try, if required.
Only the object of Throwable class or its subclasses can be thrown.
A try block must followed by a catch block or finally block or both.
The code present in the finally block executes even if the try or catch block contains control transfer statements like return, break or continue.
Leave a Comment