1 / 21

Java II

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 .

tymon
Download Presentation

Java II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java II IHTESHAM UL HAQ

  2. Exception Handling “If anything can go wrong, it will.” IHTESHAM UL HAQ

  3. • What is an “Exception”? • exception • An event during program execution that prevents the program from continuing normally. • Generally, an error. IHTESHAM UL HAQ

  4. • Other languages react to errors by crashing. • Java manages errors in a controlled way. • Java permits recovery, if at all possible. IHTESHAM UL HAQ

  5. • 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

  6. • 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

  7. • 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

  8. When To Use Exception Handling IHTESHAM UL HAQ

  9. • 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

  10. try, throw and catch IHTESHAM UL HAQ

  11. • Keywords • Java supports exception handling with these keywords: • try, • throw and • catch. IHTESHAM UL HAQ

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. The finally Block IHTESHAM UL HAQ

  19. 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

  20. 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

  21. 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

More Related