Docsity

Java Exception Handling: Understanding Try, Catch, and Throw, Slides of Advanced Computer Programming

Partial preview of the text

Download Java Exception Handling: Understanding Try, Catch, and Throw and more Slides Advanced Computer Programming in PDF only on Docsity!

A Third Look At Java

Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 1

A Little Demo

Chapter Seventeen Modern Programming Languages, 2nd ed. 2

public class Test { public static void main(String[] args) { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); } }

javac Test.java java Test 6 3 2

Docsity.com

Outline

 17.2 Throwable classes

 17.3 Catching exceptions

 17.4 Throwing exceptions

 17.5 Checked exceptions

 17.6 Error handling

 17.7 Finally

 17.8 Farewell to Java

Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Some Predefined Exceptions

Chapter Seventeen Modern Programming Languages, 2nd ed. 5

Java Exception Code to Cause It

NullPointerException String s = null; s.length(); ArithmeticException int a = 3; int b = 0; int q = a/b; ArrayIndexOutOfBoundsException int[] a = new int[10]; a[10];

ClassCastException Object x = new Integer(1); String s = (String) x; StringIndexOutOfBoundsException String s = "Hello"; s.charAt(5);

Docsity.com

Throwable Classes

 To be thrown as an exception, an object

must be of a class that inherits from the

predefined class Throwable

 There are four important predefined classes

in that part of the class hierarchy:

– Throwable

– Error

– Exception

– RuntimeException

Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 7

Chapter Seventeen Modern Programming Languages, 2nd ed. 8

Java will only throw objects of a class descended from Throwable

Classes derived from Error are used for serious, system- generated errors, like OutOfMemoryError, that usually cannot be recovered from

Classes derived from Exception are used for ordinary errors that a program might want to catch and recover from

Classes derived from RuntimeException are used for ordinary system- generated errors, like ArithmeticException

Exception

Object

Throwable

Error

RuntimeException

…

…

…

Docsity.com

The try Statement

 Simplified… full syntax later

 The < type > is a throwable class name

 Does the try part

 Does the catch part only if the try part

throws an exception of the given < type >

Chapter Seventeen Modern Programming Languages, 2nd ed. 10

< try-statement > ::= < try-part > < catch-part >

< try-part > ::= try < compound-statement >

< catch-part > ::= catch (< type > < variable-name >)

< compound-statement >

Docsity.com

Example

Chapter Seventeen Modern Programming Languages, 2nd ed. 11

public class Test { public static void main(String[] args) { try { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); } catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); } } } This will catch and handle any^ ArithmeticException. Other exceptions will still get the language system’s default behavior.

Docsity.com

After The try Statement

 A try statement can be just another in a

sequence of statements

 If no exception occurs in the try part, the

catch part is not executed

 If no exception occurs in the try part, or if

there is an exception which is caught in the

catch part, execution continues with the

statement following the try statement

Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 13

Exception Handled

Chapter Seventeen Modern Programming Languages, 2nd ed. 14

System.out.print("1, "); try { String s = null; s.length(); } catch (NullPointerException e) { System.out.print("2, "); } System.out.println("3");

This just prints the line

Docsity.com

Example

 If g throws an ArithmeticException,

that it does not catch, f will get it

 In general, the throw and the catch can be

separated by any number of method

invocations

Chapter Seventeen Modern Programming Languages, 2nd ed. 16

void f() {

try {

g();

catch (ArithmeticException a) {

Docsity.com

 If z throws an exception

it does not catch, z’s

activation stops…

 …then y gets a chance to

catch it; if it doesn’t, y’s

activation stops…

 …and so on all the way

back to f

Chapter Seventeen Modern Programming Languages, 2nd ed. 17

y’s activation record

z’s activation record

f’s activation record

g’s activation record

...

Docsity.com

Multiple catch Parts

 To catch more than one kind of exception, a

catch part can specify some general

superclass like RuntimeException

 But usually, to handle different kinds of

exceptions differently, you use multiple

catch parts

Chapter Seventeen Modern Programming Languages, 2nd ed. 19

< try-statement > ::= < try-part > < catch-parts >

< try-part > ::= try < compound-statement >

< catch-parts > ::= < catch-part > < catch-parts >

| < catch-part >

< catch-part > ::= catch (< type > < variable-name >)

< compound-statement >

Docsity.com

Example

Chapter Seventeen Modern Programming Languages, 2nd ed. 20

public static void main(String[] args) { try { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); } catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); } catch (ArrayIndexOutOfBoundsException a) { System.out.println("Requires two parameters."); } } (^) This will catch and handle both ArithmeticException and ArrayIndexOutOfBoundsException

Docsity.com