1 / 26

Advanced Flow of Control : Introduction

Advanced Flow of Control : Introduction. This chapter focuses on: exception processing catching and handling exceptions creating new exceptions exception analysis. Exceptions( 예외상황 ). An exception is an object that describes an unusual situation

freemanc
Download Presentation

Advanced Flow of Control : Introduction

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. Advanced Flow of Control : Introduction • This chapter focuses on: • exception processing • catching and handling exceptions • creating new exceptions • exception analysis

  2. Exceptions(예외상황) • An exception is an object that describes an unusual situation • Exceptions are thrown by a program or runtime environment, and • Thrown exceptions may be caughtand handledby another part

  3. 예외상황 발생 • Runtime exception • irrecoverable exception • thrown by the runtime library code, not your code • User-defined exception • by throw statement. • you have to make provision for catching thrown exceptions

  4. 예외상황 정의 • Exception object is a first-class object • 일반 object처럼 클래스를 이용하여 정의되고 • 일반 object처럼 사용될 수 있다. • 일반 object와 차이점 • throw 될 수 있다. • 예외상황을 정의하는 클래스 • Exception class를 상속받아서 정의해야 한다.

  5. Object Throwable Exception Error RuntimeException ClassNotFoundException IllegalAcessException InstantiationException InterruptedException NoSuchMethodException ….. ArithmeticException NagativeArraySizeException ArrayIndexOutOfBoundsException SecurityException …

  6. Execution Flow • A program can be separated into • a normal execution flow and • an exception execution flow

  7. Exception Handling • A program can deal with an exception in one of three ways: (1) ignore it (2) handle it where it occurs (3) handle it an another place in the program

  8. Exception Handling • If an exception is ignored by the program, • The program will terminate and produce a message • The message includes a call stack trace • main 메쏘드부터 호출과정 • See Zero.java

  9. Example: Zero.java public class Zero { public static void main (String[] args) { int numerator = 10; int denominator = 0; System.out.println(numerator/denominator); // cann’t reach here System.out.println(“Cann’t reach here”); } // method main } // class Zero

  10. The throw Statement • A programmer can define an exception by extending the appropriate class • Exceptions are thrown using throw statement: throw exception-object; • Usually a throw statement is nested inside an if statement

  11. Example: Throw_Demo.java import java.io.IOException; public class Throw_Demo { public static void main (String[] args) throws Ooops { Ooops problem = new Ooops ("Alert!"); throw problem; // execution never gets to this point } // method main } // class Throw_Demo class Ooops extends IOException { Ooops (String message) { super (message); } // constructor Ooops } // class Ooops

  12. The try-catch Statement • To process an exception when it occurs try { … } catch (E1 x) { … } … catch (En x) { … } • When an exception occurs in the try block, processing continues at the first catch clause that matches the exception type

  13. Example: Adding.java import java.io.*; public class Adding { public static void main (String[] args) { int num1 = User_Reader.get_integer ("Enter a number:"); int num2 = User_Reader.get_integer ("Enter another number: "); System.out.println ("The sum is " + (num1+num2)); } // method main } // class Adding

  14. class User_Reader { public static int get_integer (String prompt) { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); int number = 0; boolean valid = false; while (! valid) { System.out.print (prompt); System.out.flush (); try { number = Integer.parseInt (stdin.readLine()); valid = true; } catch (NumberFormatException exception) { System.out.println ("Invalid input. Try again."); } catch (IOException exception) { System.out.println ("Input problem. Terminating."); System.exit(0); } } return number; } // method get_integer } // class User_Reader

  15. The finally Clause • A try statement can have an optional clause finally try { … } catch (E1 x) { … } … catch (En x) { … } finally { … } • The statements in the finally clause are executed whether exception is generated or not

  16. Example: ThrowDemo.java class ThrowDemo{ public static void main(String arg[]){ try{ System.out.println("a() 메서드 이전"); a(); System.out.println("a() 메서드 이후"); } catch(Exeception e){ System.out.println("main:" + e); } finally{ System.out.println("main:finally 블록"); } }

  17. public static void a(){ try{ System.out.println("throw문 이전"); throw new ArithmeticException(); } catch(Exception e){ System.out.println("a: " + e); } finally{ System.out.println("a:finally 블록"); } }//method main }//class ThrowDemo

  18. Exception Propagation • Exceptions propagate up through the method calling hierarchy • until they are caught and handled or • until they reach the outermost level

  19. Exception Propagation Chain of call at runtime Main() Apple(); Orange()

  20. Source program 6 Then here enclosing the call to apple() Main(){ apple(); } apple(){ orange(); } orange(){ int i, j =0; ... i=i/j; } 5 Then here surrounding the call to orange() 4 3 Runtime system look for try…catch statement in orange 2 Exception occur! 1

  21. Example: Propagation_Demo.java public class Propagation_Demo { static public void main (String[] args) { Exception_Scope demo = new Exception_Scope(); System.out.println("program beginning"); demo.level1(); System.out.println("program ending"); } // method main } // class Propagation_Demo

  22. class Exception_Scope { public void level3 (int adjustment) { int current = 1; System.out.println("level3 beginning"); current = current / adjustment; System.out.println("level3 ending"); } // method level3 public void level2() { System.out.println("level2 beginning"); level3 (0); System.out.println("level2 ending"); } // method level2 public void level1() { System.out.println("level1 beginning"); try { level2(); } catch (ArithmeticException problem) { System.out.println (problem.getMessage()); problem.printStackTrace(); } System.out.println("level1 ending"); } // method level1 } // class Exception_Scope

  23. Exceptions • An exception is either checked or unchecked • The compiler will complain if a checked exception is not caught appropriately • An unchecked exception does not require explicit handling • A checked exception can only be thrown • within a try block or • within a method specified to throw the exception

  24. Specification of uncaught exceptions • Specify uncaught exceptions in method definition m(...) throws A, B, …, C { … }

  25. Example: Spec.java import java.io.*; public class Spec{ public static void main (String[] args)throws IOException{ System.out.println("jylee....!!"); int ch = System.in.read(); System.out.println((char)ch); } }

  26. Uncaught Exception Analysis in JDK • Intraprocedural analysis • Based on programmer’s specifications of method definitions. • Check whether thrown exceptions are caught or specified in the method definition

More Related