Download 5-chapter-6-exceptions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 6-1
(Book Chapter 8)
Exceptions
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Animated Version
Chapter 8- 1
Objectives
• After you have read and studied this chapter, you
should be able to
– Improve the reliability of code by incorporating
exception-handling.
– Write methods that propagate exceptions.
– Implement the try-catch blocks for catching and
handling exceptions.
– Write programmer-defined exception classes.
– Distinguish the checked and unchecked, or runtime,
exceptions.
Definition
• An exception represents an error condition that
can occur during the normal course of program
execution.
• When an exception occurs, or is thrown, the
normal sequence of flow is terminated. The
exception-handling routine is then executed; we
say the thrown exception is caught.
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 3
Not Catching Exceptions
Scanner scanner = new Scanner(System.in);
System.out.println(“Enter integer:");
int number = scanner.nextInt();
Error message for invalid input
Exception
at
at
at
at
at
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
in thread “main” java.lang.InputMismatchException
java.util.Scanner.throwFor(Scanner.java:819)
java.util.Scanner.next(Scanner.java:1431)
java.util.Scanner.nextInt(Scanner.java:2040)
java.util.Scanner.nextInt(Scanner.java:2000)
Ch8Sample1.main(Ch8Sample1.java:35)
Chapter 8 - 4
Catching an Exception- Ex1
System.out.println(“Enter integer:");
try {
try
int number = scanner.nextInt();
} catch (InputMismatchException e){
catch
System.out.println("Invalid Entry. "
+
"Please enter digits only");
}
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 5
Catching an ExceptionEx1(with Repetition)
System.out.println(“Enter integer:");
boolean repeat=true;
while(repeat){
try {
int number = scanner.nextInt();
repeat= false;
}
catch (InputMismatchException e){
System.out.println("Invalid Entry. " +
only");
"Please enter
digits
scanner.next();
}
}
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
remove the leftover garbage input
Chapter 8 - 6
Catching an Exception- Ex2
;
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 7
Catching an Exception- Ex2
(with Repetition)
}
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 8
try-catch Control Flow
Exception
try {
<t-stmt-1>
<t-stmt-2>
No Exception
try {
<t-stmt-1>
<t-stmt-2>
Assume <t-stmt-3>
throws an exception.
<t-stmt-3>
<t-stmt-4>
. . .
<t-stmt-n>
<t-stmt-3>
} catch (Exception e) {
<c-stmt-1>
. . .
<c-stmt-m>
}
<next stmt>
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
<t-stmt-4>
Remaining
statements in the
try block is skipped.
. . .
<t-stmt-n>
} catch (Exception e) {
<c-stmt-1>
Statements in the
catch block are
executed.
And the execution
continues to the
next statement
All statements in
the try block are
executed.
. . .
<c-stmt-m>
Statements in the
catch block are
skipped.
}
<next stmt>
Chapter 8 - 9
Multiple catch Blocks
• A single try-catch statement can include multiple catch
blocks, one for each type of exception.
try {
. . .
age = scanner.nextInt( );
. . .
val = intArray[indx];
. . .
} catch (InputMismatchException e){
. . .
} catch (ArrayIndexOutOfBoundsException e){
. . .
}
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 10
Multiple catch Blocks
import java.util.*;
catch (InputMismatchException e1)
{
System.out.println(“Enter digits only”);
}
catch (ArithmeticException e2)
{
System.out.println(“Cannot divide by 0”);
}
public class ExceptionExample
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int dividend, divisor, quotient;
try
{
System.out.print(“Enter the “+ "dividend: ");
dividend = console.nextInt();
System.out.println();
}
}
System.out.print(“Enter the “+ "divisor: ");
divisor = console.nextInt();
System.out.println();
quotient = dividend / divisor;
System.out.println(“Quotient = “+ quotient);
}
Chapter 8 - 11
Multiple catch Control Flow
Exception
try {
<t-stmt-1>
<t-stmt-2>
No Exception
Assume <t-stmt-3>
throws an exception
and <catch-block-3>
is the matching block.
try {
<t-stmt-1>
<t-stmt-2>
<t-stmt-3>
<t-stmt-4>
. . .
<t-stmt-n>
<t-stmt-3>
<t-stmt-4>
Remaining
statements in the
try block is skipped.
}
All statements in
the try block are
executed and throw
no exceptions.
. . .
<t-stmt-n>
}
<catch-block-1>
<catch-block-2>
<catch-block-3>
. . .
<catch-block-m>
}
<catch-block-1>
<catch-block-2>
Statements
in the
matching
catch block
are executed.
<catch-block-3>
All catch
blocks are
skipped.
. . .
<catch-block-m>
}
<next stmt>
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
<next stmt>
Chapter 8 - 12
The finally Block
• There are situations where we need to take certain
actions regardless of whether an exception is
thrown or not.
• We place statements that must be executed
regardless of exceptions in the finally block.
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 13
try-catch-finally Control Flow
Exception
No Exception
Assume <t-stmt-i>
try {
throws an exception
<t-stmt-1>
and <catch-block-i> is
. . .
the matching block.
<t-stmt-i>
. . .
<t-stmt-n>
}
<catch-block-1>
. . .
<catch-block-i>
. . .
<catch-block-m>
try {
<t-stmt-1>
. . .
<t-stmt-i>
. . .
<t-stmt-n>
}
<catch-block-1>
. . .
<catch-block-i>
. . .
<catch-block-m>
} finally {
. . .
}
<next stmt>
} finally {
. . .
}
<next stmt>
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
finally block is
executed.
finally block is
executed.
Chapter 8 - 14
try-catch-finally Control Flow
import java.util.*;
public class ETest
{
public static void main(String args[])
{
Scanner read = new Scanner(System.in);
int x;
try{
x = read.nextInt();
}
catch (NumberFormatException e){
System.out.println(e);
}
finally{
System.out.println("The end");
}
}
}
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Sample Run:
ÏÏ«Ï ----jGRASP exec: java ETest
e
The end
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at ETest.main(ETest.java:9)
----jGRASP wedge: exit code for process is 1.
Ï©Ï --
Chapter 8 - 15
Getting Information
• There are three methods we can call to get
information about the thrown exception:
– getMessage
– printStackTrace
– toString
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 16
©The McGraw-Hill Companies, Inc. Permission
required for reproduction or display.
Chapter 8 - 17
Related documents