Monday, November 16, 2015

Exception handling in java with example

In this post you will understand and learn Exception Handling in Java. This is one of the core concept in Java. As a programming developer must have strong knowledge on exceptions handling in java. In previous post i have written Top 10 exceptions in java,recommended go through this link as well to aware of various exceptions in java.

 

Exception:

  • An Exception can be anything which interrupts normal flow of the Program.
  • It is an unexpected unwanted event which disturbs entire flow of the program
  • If we are not handling exception, the program may terminate abnormally.
  • This is not a graceful termination
  • Being a good programming practice we should handle exceptions  for graceful termination of the program

Reasons for exception:

  • Fallowing situation can cause an exception
  • Try to opening non-existing files
  • Network connection problem
  • class file missing which was supposed to be loaded and so on

Difference between error and Exception:

 


Advantages of Exception Handling:

  1. Exception Handling allows us to control normal flow of the program by using Exception handling in the  program
  2. Exception normally disrupts normal flow of the program that is why we use exception handling
Example:

Statement 1;
statement 2;
statement 3;
statement 4;//exception occurs
statement 5;
statement 6;
statement 7;

Suppose there is 7 statements in your program  and there occurs exception at statement 4,rest of the code will not be executed. statement 5 to statement 7 will not run. If we perform exception handling the rest of the code will be executed. That is why we use exception handling in java.

Exception class Hierarchy:


Types of Exceptions:

There are mainly two types of exceptions. They are

1) Checked Exception
2) Unchecked Exception

Checked Exceptions:

 The Exceptions which are checked by the compiler for smooth execution of the program
at runtime are called "Checked exception"

Examples:  IOException, InterruptedException, ServletException


Unchecked Exceptions:

The Exceptions which are unable to checked by the compiler is called "Unchecked exception".Runtime Exceptions are also known as unchecked exception

Examples:  ArithmeticException, ArrayIndexOutOfBoundException, NullPointerException

Recommended to read : TOP 10 Exceptions in java

java Exception Handling:

There are 5 keywords are used in java exception handling
  1. try
  2. catch
  3. finally
  4. throw
  5. throws

try block:

  •     we have to place the risky code inside the try block and the corresponding exception
handling code inside the catch block
  • It must be used within the method
  • java try block must be fallowed by either catch block or finally block

Syntax for try block:

try{
 // risky code that means may throw exception
   }catch(Exception class-name  ref)
{
}

Catch block:  

  • Java catch block is used to handle the exception.
  • It must be used after the try block only
  • you can use multiple catch block within single try block

Without Exception Handling Code:

Ex:
class Test{
public static void main(String args[]){
System.out.println("Statement1");
System.out.println(10/0);
System.out.println("statement2");
  }
}
output:

Abnormal termination


Solution by Exception Handling:

Ex:    
class Test{
public static void main(String args[]){
System.out.println("Statement1");
try{
System.out.println(10/0);
} 
catch(ArithmetichException ae){
System.out.println(10/2);
}  
System.out.println("statement2");
}
}

Output:   
Normal Termination


Try with multiple catch blocks:

  • The way of handling exception is valid from exception to exception
  • Hence for every exception we should define corresponding catch blocks
  • Hence try with multiple catch block is possible
 try{
 // risky code
 } 
catch(ArithmeticException ae){
 // handling to Arithmetic Exception
 }
 catch(NullPointerException npe){
 //handling Nullpointer Exception
 }
catch(IOException ioe)
{
// handling IOException
}
catch(Exception e)
{
 //handling Exception
}

Finally:

  • It is not recommended to place clean up code inside try block  because there is no guarantee for execution of all statements inside try block
  • It is not recommended to maintain clean up code inside catch block because if there is no execution the catch block will not be executed
  • we required a block to maintain clean up code which should execute always irrespective of whether exception is raised or not whether it is handle or not. such block is nothing but"finally block"
  • Hence the main objective of finally block is to maintain clean up code
EX:

try{
//open the database connection
// read the data
catch(X e)
{
}
finally{
// close the connection
}


I hope you enjoy this post and share this to your friends and social website to reach more people. Keep follow me for latest updates and let me know your comments about this concept.







 

1 comment:

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...