Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
Archana Priyadarshini
Archana Priyadarshini
Computer Science
14 Jan 2019

Exception handling in java and it's advantages

Exception Handling is the mechanism to handle runtime errors or malfunctions. It is needed to prevent abrupt termination of program. 

In Java exception handling uses the following keywords

  1. try
  2. catch
  3. throw
  4. throws
  5. finally

Exception handling is done by transferring the execution to an appropriate exception handler when an exception is encountered.

Advantages Of Exception Handling

  1. The flow of the execution is not terminated if the exception is handled
  2. The error can also be identified using catch
  3. Separating the error handling code and program code
  4. ability to propagate errors up the call stack

Example of exception handling using try..catch


  1.   public class TryCatchExample
  2.  public static void main(String[] args) {  
  3.         try  
  4.         {  
  5.         int x=50/0;
  6.         }  
  7.         catch(ArithmeticException e)  
  8.         {  
  9.             System.out.println(e);  
  10.         }  
  11.         System.out.println("...");  //Rest of the code
  12.     }  
  13. }