Modal Box

Exception Handling in java

Exception is the abnormal condition that disrupts the flow of the program.
Java has following five blocks for exception handling.
1. try
2. catch
3. finally
4. throw
5. throws

java has two type of exceptions, checked and unchecked exceptions :

 Checked Exception:

Checked exceptions are checked at compile time and need (must) to be catched .
e.g. SQLException , IOException etc.

 Unchecked Exception:

Uncheked exceptions are checked at runtime, not at the compile time            e.g. ArithmeticException,
ArrayIndexOutOfBoundsException etc.

Example of exception program using try catch block :

public class ExceptionDemo
{
public static void main(String args[])
{
try
{
int a=100;
int b=0;
int c=a/b;
}
catch(ArithmeticException ex)
{
System.out.println(ex);
}
System.out.println("this code will execute after catch");
}
}
⇒ Here, try block is the suspicious part which causes an exception to occur.
⇒ catch block executes only if there is an exception in try block.

A try block may have multiple catch blocks and the catch related to exception executes.

public class MultipleCatchExceptionDemo
{ 
  public static void main(String args[])
  { 
    try
    { 
      int a=Integer.parseInt(args[0]);
      int b=Integer.parseInt(args[1]);
      int c=a/b; 
    }
     
catch(ArithmeticException ex) { System.out.println("Exception arithmetic"+ex); } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("Exception Array out bounds"+ex); } } }

⇒ In this case, if we do not pass two no’s as command line arguments, then the second exception block executes.
⇒ if the 2nd no. ( b) is passed as 0, then the first exception block division by zero executes.

finally block in java :

In java each try block must have either catch or finally or both blocks. The main difference is that the catch executes if there is an exception but the finally block always executes whether there is exception or not.

Example of try using with catch and finally:

public class CatchFinallyDemo
{ 
 public static void main(String args[])
 { 
  try
  { 
    int a=Integer.parseInt(args[0]);
    int b=Integer.parseInt(args[1]);
    int c=a/b; 
  }
    catch(ArithmeticException ex)
    {
      System.out.println("This block executes only if there is an exception ie b no as zero");
    }
    finally
    {
      System.out.println("This block always executes if there is an exception or not");
    }
  } 
}

Nested try block : try block may be nested

public class NestedTryDemo
{
public static void main(String args[])
{
try
{
int a=0;
int b=0;
try
{
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
int c=a/b;
}
catch(Exception ex)//here exception is the super class of all exception class so it may handle all exceptions.
{
System.out.println("catch block for inner try");
}
}
catch(Exception ex)
{
System.out.println("catch block for outer try");
}

}
}

throws in java :

throws keyword may be used as an alternative of catch to handle checked exceptions.
The use of throws is shown as following :

import java.sql.*;
public class ThrowsExceptionDemo
{
 public static void main(String args[])throws SQLException
 {
   class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //code for database connectivity, checked exception must be caught.
 }
}

Custom Exceptions :

User Defined Exception may be created by inheriting Exception class.
use of throw keyword will also be implemented in this example.

class NegativeAgedException
{
  NegativeAgedException(int age)
  {
   System.out.println("this is negative age"+age);
  }
}

class ExceptionMain { public static void main(String args[]) { int age=Integer.parseInt(args[0]); if(age<0) { throw new NegativeAgedException(age); } } }

 

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial