Top Banner
'99 정정정정정 정정정정 1 Advanced Flow of Control : Introduction This chapter focuses on: exception processing catching and handling exceptions creating new exceptions exception analysis
26

1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

Jan 14, 2016

Download

Documents

Horatio Murphy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

'99 정보과학회 튜토리얼 1

Advanced Flow of Control : Introduction

• This chapter focuses on:– exception processing– catching and handling exceptions– creating new exceptions– exception analysis

Page 2: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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 caught and handled by

another part

Page 3: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 4: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

4

예외상황 정의

• Exception object is a first-class object

– 일반 object 처럼 클래스를 이용하여 정의되고

– 일반 object 처럼 사용될 수 있다 .

• 일반 object 와 차이점

– throw 될 수 있다 .

• 예외상황을 정의하는 클래스

– Exception class 를 상속받아서 정의해야 한다 .

Page 5: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

5

Object

Throwable

Exception Error

RuntimeExceptionClassNotFoundException

IllegalAcessException

InstantiationException

InterruptedException

NoSuchMethodException

…..

ArithmeticException

NagativeArraySizeException

ArrayIndexOutOfBoundsException

SecurityException

Page 6: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

6

Execution Flow

• A program can be separated into

– a normal execution flow and

– an exception execution flow

Page 7: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 8: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 9: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

9

public class Zero { public static void main (String[] args) {

int numerator = 10; int denominator = 0;

System.out.println(numerator/denominator);

// cann’t reach hereSystem.out.println(“Cann’t reach here”);

} // method main

} // class Zero

Example: Zero.java

Page 10: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 11: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 12: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 13: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 14: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 15: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 16: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

16

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 블록 ");}

}

Example: ThrowDemo.java

Page 17: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 18: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

18

Exception Propagation

• Exceptions propagate up through the method

calling hierarchy

– until they are caught and handled or

– until they reach the outermost level

Page 19: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

19

Exception Propagation

Chain of callat runtime Main()

Apple();

Orange()

Page 20: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

20

Source program

Main(){ apple();}

apple(){ orange();}orange(){ int i, j =0; ... i=i/j;} Exception occur!

Runtime system look for try…catch statement in orange

Then here surrounding the call to orange()

Then here enclosing the call to apple()

1

2

34

5

6

Page 21: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 22: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 23: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

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

Page 24: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

24

Specification of uncaught exceptions

• Specify uncaught exceptions in method

definition

m(...) throws A, B, …, C

{ … }

Page 25: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

25

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);

}}

Example: Spec.java

Page 26: 1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

26

Uncaught Exception Analysis in JDK

• Intraprocedural analysis

– Based on programmer’s specifications of method

definitions.

– Check whether thrown exceptions are caught or s

pecified in the method definition