KEMBAR78
Exception handling in java | PPTX
EXCEPTION HANDLING
IN JAVA
Tips and tools for creating and presenting wide format
slides
 The exception handling in java is one of the
powerful mechanism to handle the runtime
errors so that normal flow of the application
can be maintained.
Types of Exception
 Checked Exception
 Unchecked Exception
 Error
 Checked Exception:
 The classes that extend Throwable class
except RuntimeException and Error are known
as checked exceptions e.g. IOException,
SQLException etc. Checked exceptions are
checked at compile-time.
 Unchecked Exception:
 The classes that extend RuntimeException are
known as unchecked exceptions e.g.
ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time rather
they are checked at runtime.
 Error
 Error is irrecoverable e.g. OutOfMemoryError,
VirtualMachineError, AssertionError etc.
 Common scenarios where exceptions may
occur
 Scenario where ArithmeticException occurs
 Scenario where NullPointerException occurs
 Scenario where NumberFormatException
occurs
 Scenario where
ArrayIndexOutOfBoundsException occurs
 Java Exception Handling Keywords
 Java try-catch:
 Java try block
 Java try block is used to enclose the code that
might throw an exception. It must be used
within the method.
 Java try block must be followed by either catch
or finally block.
 Syntax of java try-catch
 Try
 {
 //code that may throw exception
 }
 catch(Exception_class_Name ref)
 {}
 Syntax of try-finally block:
 Try
 {
 //code that may throw exception
 }
 Finally
 {}
 Java 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 with a single
try.
 Problem without exception handling
 public class Testtrycatch1
 {
 public static void main(String args[])
 {
 int data=50/0;//may throw exception
 System.out.println("rest of the code...");
 }
 }
 Solution by exception handling
 public class Testtrycatch2{
 public static void main(String args[]){
 try{
 int data=50/0;
 }
 catch(ArithmeticException e){System.out.println(e
);}
 System.out.println("rest of the code...");
 } }
 Internal working of java try-catch block
 Java catch multiple exceptions
 public class TestMultipleCatchBlock{
 public static void main(String args[]){
 try
 {
 int a[]=new int[5];
 a[5]=30/0;
 }
 catch(ArithmeticException e){System.out.println("task1 is complet
ed");}
 catch(ArrayIndexOutOfBoundsException e){System.out.println("ta
sk 2 completed");}
 catch(Exception e){System.out.println("common task completed")
;}

 System.out.println("rest of the code...");
 } }
 Rule: At a time only one Exception is occured
and at a time only one catch block is executed.
 Rule: All catch blocks must be ordered from
most specific to most general i.e. catch for
ArithmeticException must come before catch
for Exception .
 class TestMultipleCatchBlock1{
 public static void main(String args[]){
 try
 {
 int a[]=new int[5];
 a[15]=30;
 }
 catch(Exception e){System.out.println("common task completed
");}
 catch(ArithmeticException e){System.out.println("task1 is compl
eted");}
 catch(ArrayIndexOutOfBoundsException e){System.out.println("
task 2 completed");}
 System.out.println("rest of the code...");
 Java Nested try block
 The try block within a try block is known as
nested try block in java.
 Why use nested try block
 Sometimes a situation may arise where a part
of a block may cause one error and the entire
block itself may cause another error. In such
cases, exception handlers have to be nested.
 try
 {
 statement 1;
 try
 {
 statement 1;
 }
 catch(Exception e)
 {
 }
 }
 catch(Exception e)
 {
 }
 Java finally block:
 Java finally block is a block that is used to
execute important code such as closing
connection, stream etc.
 Java finally block is always executed whether
exception is handled or not.
 Java finally block follows try or catch block.
 Note: If you don't handle exception, before
terminating the program, JVM executes finally
block(if any).
 Why use java finally
 Finally block in java can be used to put
"cleanup" code such as closing a file, closing
connection etc.
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data); }
catch(NullPointerException e){System.out.println(e)
;}
finally{System.out.println("finally block is always ex
ecuted");}
System.out.println("rest of the code...");
} }
 Rule: For each try block there can be zero
or more catch blocks, but only one finally
block.
 Java throw keyword:
 The Java throw keyword is used to explicitly
throw an exception.
 We can throw either checked or uncheked
exception in java by throw keyword.
 The syntax of java throw keyword is given
below.
 throw exception;
 Example of throw IOException.
 throw new IOException("sorry device error);
 Java throw keyword example
 In this example, we have created the validate
method that takes integer value as a
parameter. If the age is less than 18, we are
throwing the ArithmeticException otherwise
print a message welcome to vote.
 public class TestThrow1{
 static void validate(int age){
 if(age<18)
 throw new ArithmeticException("not valid");
 else
 System.out.println("welcome to vote");
 }
 public static void main(String args[]){
 validate(13);
 System.out.println("rest of the code..."); } }
 Java throws keyword
 The Java throws keyword is used to declare
an exception. It gives an information to the
programmer that there may occur an exception
so it is better for the programmer to provide
the exception handling code so that normal
flow can be maintained.
 Exception Handling is mainly used to handle
the checked exceptions. If there occurs any
unchecked exception such as
NullPointerException, it is programmers fault
that he is not performing check up before the
code being used.
 Syntax of java throws:
 returnType method() throws exceptionClassNam
e
 {
 //method code
 }
 Java throws example

Exception handling in java

  • 1.
    EXCEPTION HANDLING IN JAVA Tipsand tools for creating and presenting wide format slides
  • 2.
     The exceptionhandling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
  • 3.
    Types of Exception Checked Exception  Unchecked Exception  Error
  • 4.
     Checked Exception: The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.
  • 5.
     Unchecked Exception: The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
  • 6.
     Error  Erroris irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • 7.
     Common scenarioswhere exceptions may occur
  • 8.
     Scenario whereArithmeticException occurs  Scenario where NullPointerException occurs  Scenario where NumberFormatException occurs  Scenario where ArrayIndexOutOfBoundsException occurs
  • 9.
     Java ExceptionHandling Keywords
  • 10.
     Java try-catch: Java try block  Java try block is used to enclose the code that might throw an exception. It must be used within the method.  Java try block must be followed by either catch or finally block.
  • 11.
     Syntax ofjava try-catch  Try  {  //code that may throw exception  }  catch(Exception_class_Name ref)  {}
  • 12.
     Syntax oftry-finally block:  Try  {  //code that may throw exception  }  Finally  {}
  • 13.
     Java catchblock:  Java catch block is used to handle the Exception. It must be used after the try block only.  You can use multiple catch block with a single try.
  • 14.
     Problem withoutexception handling
  • 15.
     public classTesttrycatch1  {  public static void main(String args[])  {  int data=50/0;//may throw exception  System.out.println("rest of the code...");  }  }
  • 16.
     Solution byexception handling
  • 17.
     public classTesttrycatch2{  public static void main(String args[]){  try{  int data=50/0;  }  catch(ArithmeticException e){System.out.println(e );}  System.out.println("rest of the code...");  } }
  • 18.
     Internal workingof java try-catch block
  • 20.
     Java catchmultiple exceptions
  • 21.
     public classTestMultipleCatchBlock{  public static void main(String args[]){  try  {  int a[]=new int[5];  a[5]=30/0;  }  catch(ArithmeticException e){System.out.println("task1 is complet ed");}  catch(ArrayIndexOutOfBoundsException e){System.out.println("ta sk 2 completed");}  catch(Exception e){System.out.println("common task completed") ;}   System.out.println("rest of the code...");  } }
  • 22.
     Rule: Ata time only one Exception is occured and at a time only one catch block is executed.  Rule: All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception .
  • 23.
     class TestMultipleCatchBlock1{ public static void main(String args[]){  try  {  int a[]=new int[5];  a[15]=30;  }  catch(Exception e){System.out.println("common task completed ");}  catch(ArithmeticException e){System.out.println("task1 is compl eted");}  catch(ArrayIndexOutOfBoundsException e){System.out.println(" task 2 completed");}  System.out.println("rest of the code...");
  • 24.
     Java Nestedtry block  The try block within a try block is known as nested try block in java.
  • 25.
     Why usenested try block  Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested.
  • 26.
     try  { statement 1;  try  {  statement 1;  }  catch(Exception e)  {  }  }  catch(Exception e)  {  }
  • 27.
     Java finallyblock:  Java finally block is a block that is used to execute important code such as closing connection, stream etc.  Java finally block is always executed whether exception is handled or not.  Java finally block follows try or catch block.
  • 29.
     Note: Ifyou don't handle exception, before terminating the program, JVM executes finally block(if any).
  • 30.
     Why usejava finally  Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.
  • 31.
    class TestFinallyBlock{ public staticvoid main(String args[]){ try{ int data=25/5; System.out.println(data); } catch(NullPointerException e){System.out.println(e) ;} finally{System.out.println("finally block is always ex ecuted");} System.out.println("rest of the code..."); } }
  • 32.
     Rule: Foreach try block there can be zero or more catch blocks, but only one finally block.
  • 33.
     Java throwkeyword:  The Java throw keyword is used to explicitly throw an exception.  We can throw either checked or uncheked exception in java by throw keyword.
  • 34.
     The syntaxof java throw keyword is given below.  throw exception;
  • 35.
     Example ofthrow IOException.  throw new IOException("sorry device error);
  • 36.
     Java throwkeyword example
  • 37.
     In thisexample, we have created the validate method that takes integer value as a parameter. If the age is less than 18, we are throwing the ArithmeticException otherwise print a message welcome to vote.
  • 38.
     public classTestThrow1{  static void validate(int age){  if(age<18)  throw new ArithmeticException("not valid");  else  System.out.println("welcome to vote");  }  public static void main(String args[]){  validate(13);  System.out.println("rest of the code..."); } }
  • 39.
  • 40.
     The Javathrows keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.  Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.
  • 41.
     Syntax ofjava throws:  returnType method() throws exceptionClassNam e  {  //method code  }
  • 42.