210 likes | 356 Views
Java II. Exception Handling. “If anything can go wrong, it will.”. • What is an “ Exception ”?. exception An event during program execution that prevents the program from continuing normally. Generally, an error. • Other languages react to errors by crashing .
E N D
Java II IHTESHAM UL HAQ
Exception Handling “If anything can go wrong, it will.” IHTESHAM UL HAQ
• What is an “Exception”? • exception • An event during program execution that prevents the program from continuing normally. • Generally, an error. IHTESHAM UL HAQ
• Other languages react to errors by crashing. • Java manages errors in a controlled way. • Java permits recovery, if at all possible. IHTESHAM UL HAQ
• What users expect when an error happens: —tell them an error happened —save all their work-in-progress, if possible. —allow them to exit the program gracefully. IHTESHAM UL HAQ
• In Java, when an error happens, we say an exception has been thrown. • Once an exception has been thrown, the JVM begins a searchfor some logic able to handle the exception. IHTESHAM UL HAQ
• The bit of logic able to handle the exception is called, naturally, an exception handler. • When the JVM finds the right exception handler, we say the exception has been caught. • Control is then transferred to the “catching block.” IHTESHAM UL HAQ
When To Use Exception Handling IHTESHAM UL HAQ
• Warning! Exception handling should only be used for exceptional situations. Your program must try to prevent exceptions—with normal programming techniques—such as bounds checking on arrays or validation of input data. Exception Handling is the last line of defense. IHTESHAM UL HAQ
try, throw and catch IHTESHAM UL HAQ
• Keywords • Java supports exception handling with these keywords: • try, • throw and • catch. IHTESHAM UL HAQ
public class NoExceptionHandling { public static void main( String[] args ) { int x = 1, y = 0, z = 0; z = x / y; System.out.println( “x/y = “ + z ); } } Division by zero. This will never print, nor will it tell you why it failed. The program just dies. It has no exception handling. IHTESHAM UL HAQ
public class HasExceptionHandling { public static void main( String[] args ) { int x = 1, y = 0, z = 0; try { z = x / y; System.out.println( “Not executed”); } catch( Exception e ) { System.out.println( “Exception!”); } System.out.println( “x/y = “ + z ); } } IHTESHAM UL HAQ
public class HasExceptionHandling { public static void main( String[] args ) { int x = 1, y = 0, z = 0; try { z = x / y; System.out.println( “Not executed”); } catch( Exception e ) { System.out.println( “Exception!”); } System.out.println( “x/y = “ + z ); } } Any code you think might throw an exception should be enclosed in a “try” block. When an exception happens, the remainder of thetryblock is abandoned. Variables go out of scope. No return is possible. Every try block must be paired with at least one “catch” block—often more than one. The catch block will be executed only if an exception occurs. IHTESHAM UL HAQ
public class HasExceptionHandling { public static void main( String[] args ) { int x = 1, y = 0, z = 0; try { z = x / y; } catch( Exception e ) { System.out.println( “Exception!”); } System.out.println( “x/y = “ + z ); } } The Sequence: i.) A particular exception occurs. ii.) The runtime system generates an exception object that matches the exception that occurred. iii.) Then, the runtime system goes looking for the nearest catch block that can handle that specific type of exception. If the nearest catch block doesn’t match the exception that happened, the runtime looks beyond this method for another catch block. But, in this case, Exception is the Superclass for all exceptions, so it always matches. Exception! x/y = 0 IHTESHAM UL HAQ
public class HasExceptionHandlingWrongCatch { public static void main( String[] args ) { int x = 1, y = 0, z = 0; try { z = x / y; } catch( NullPointerException e ) { System.out.println( “Exception”); } System.out.println( “x/y = “ + z ); } } In this case, however, the catch block is expecting a different type of exception. So, this catch block doesn’t catch the error. The exception is NOT caught. The program crashes. The next statement after the catch block isnotexecuted. IHTESHAM UL HAQ
public class HasExceptionHandling { public static void main( String[] args ) { int x = 1, y = 0, z = 0; try { z = x / y; } catch( NullPointerException npe ) { System.out.println( “Null Exception!”); } catch( DivideByZeroException dbze ) { System.out.println( “DivideByZeroException!” + dbze.toString() ); } System.out.println( “Whoops!” ); } } This one doesn’t match, so it is skipped. This one does match, so it is executed. Since the exception was caught, execution can resume. IHTESHAM UL HAQ
The finally Block IHTESHAM UL HAQ
The finally Block • Java exception handling offers a third optional twist on the try-catch block: • The finally block offers you a place to put code that must always be executedno matter what—exception or no exception. • Remember: if you use a try, then you must use a catch . • The finally is optional . IHTESHAM UL HAQ
The finally Block • The finally block is like the Mafia—if you invite it in your code, it’s there whether you end up needing it or not. • So, if your try block works perfectly—then the code in the finally block gets executed. • And… if your try block throws an exception and the catch block catches the exception—then the code in the finally block still gets executed. IHTESHAM UL HAQ
public class HasFinallyBlockToo { public static void main( String[] args ) { int x = 1, y = 0, z = 0; try { z = x / y; } catch( NullPointerException e ) { System.out.println( “Exception”); } finally { System.out.println( “Always!”); } } } Always, Always Always executes! IHTESHAM UL HAQ