Customized Exception Handing by using try catch - rahul00773/JavaConcepts GitHub Wiki

It is highly recommended to handle exceptions. The code which may raise an exception is called risky code and we have to define that code inside try block. And corresponding handling code we have to define inside the catch block.

try{ risky code

}

catch(Exception e){

Handling code }

Without try-catch:

class Test{

public static void main(String[] args){

System.out.println("statement1");  
System.out.println(10/0);               // Abnormal termitation
System.out.println("statement2");


}

}

With try-catch:

class Test{

public static void main(String[] args){

System.out.println("statement1");
try{  
System.out.println(10/0);   
}            // Normal termitation
catch(Exception e){
System.out.println(10/0); 
}
System.out.println("statement2");


}

}```



Case1: if no exception occurs then the program will run normally and catch block won't execute.

try{

sout("st1");
sout("stt2");
sout("stm3");
}
catch(Exception e){
sout("stm4");
}
sout("stm5");}


Case2: If an exception raised at statement 2 and corresponding catch block matched. Then the program will stop normally
Case3: If an exception raised at statement 2 and corresponding catch block not matched. Then the program will stop abnormally
Case4: If an exception raised at statement 4 or statement 5 then it is always abnormal termination


Note: Within the try block if anywhere exception raised then the rest of the try block won't be executed even though we handled that exception. Hence within the try block, we have to take only risky code and the length of try block should be as less as possible.

2. In addition, to try block, there may be chance of raising exception inside catch and finally blocks

3. If any statement which is not part of the try block and raises an exception then it is always abnormal termination.


## Methods to print Exception Information

Throwable class defines the following methods to print exception information. 

|Method          |     PrintbleFormat                             |
|----------------|------------------------------------------------|
|printStackTrace | Name of Exception: Description StackTrace      |
|toString()      |Name of Exception: Description                  |
|getMessage()    | Description                                    |


Internally default exception handler will use the printStackTrace method to use print stack trace.


## Try with MultipleCatchBlocks
The way of handling an exception is wary from exception to exception. Hence for every exception type, it is highly recommended to separate catch block. That with multiple catch blocks is always possible and recommended to use.

try{
RiskyCode
}
catch(Exception e){
// Any kind of exception -- Worst programming practice

}

try{
RiskyCode
}
catch(ArthmeticException e){
alternative operation

}
catch(SQLException e){
use mysqlDb instead of oracle

}
catch(FileNotFoundException e){
Use Alternate DB

}

catch(Exception e){
Default Exception handling
}



If try with multiple catch blocks present then the order of catch block is very important. We have to take child first and then parent. Otherwise we will get compile time error saying : Exception xxx has already been caught.

We can't declare two catch blocks for the same exception otherwise we will get a compile-time error.

try{
RiskyCode
}
catch(ArthmeticException e){
alternative operation

}

catch(ArthmeticException e){
alternative operation

}

Exception: Java.lang.arthmeticException has already been caught