Download Exceptions and Assertions in Java: Handling Errors and Ensuring Code Correctness and more Lecture notes Java Programming in PDF only on Docsity!
LECTURE 08: EXCEPTIONS &
ASSERTIONS
o Exception
o Handle exception
o Checked vs Unchecked
o Assert
PLAN
Not Catching Exceptions
Scanner scanner = new Scanner(System.in);
System.out.println(“Enter integer:");
int number = scanner.nextInt();
Exception in thread “main” java.lang.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:819) at java.util.Scanner.next(Scanner.java:1431) at java.util.Scanner.nextInt(Scanner.java:2040) at java.util.Scanner.nextInt(Scanner.java:2000) at Ch8Sample1.main(Ch8Sample1.java:35) Error message for invalid input
Catching an Exception
System.out.print(prompt);
try {
age = scanner.nextInt( );
} catch (InputMismatchException e){
System.out.println("Invalid Entry. "
+ "Please enter digits only");
try catch
o There are two methods we can call to get information about the
thrown exception:
- getMessage
- printStackTrace
Getting Information
try {
} catch (InputMismatchException e){
scanner.next(); //remove the leftover garbage
char
System.out.println(e.getMessage());
e.printStackTrace();
o A single try-catch
statement can
include multiple
catch blocks, one
for each type of
exception.
Multiple catch Blocks
try {
age = scanner.nextInt( );
val = cal.get(id); //cal is a GregorianCalendar
} catch (InputMismatchException e){
} catch (ArrayIndexOutOfBoundsException e){
o There are situations where we need to take certain actions
regardless of whether an exception is thrown or not.
o We place statements that must be executed regardless of
exceptions in the finally block.
The finally Block
try-catch-finally Control Flow
No Exception try { <t-stmt-1>
... <t-stmt-i> ... <t-stmt-n> } <catch-block-1> ... <catch-block-i> ... <catch-block-m> } finally { ... } Assume <t-stmt-i> throws an exception and <catch-block-i> is the matching block. Exception try { <t-stmt-1> ... <t-stmt-i> ... <t-stmt-n> } <catch-block-1> ... <catch-block-i> ... <catch-block-m> } finally { ... } finally block is executed. finally block is executed.
o We can write a method that throws an exception directly, i.e., this
method is the origin of the exception.
o Use the throw reserved to create a new instance of the Exception
or its subclasses.
o The method header includes the reserved word throws.
Throwing Exceptions
public void doWork(int num) throws Exception {
if (num != val) throw new Exception("Invalid val");
o When a method may throw an exception, either directly or
indirectly, we call the method an exception thrower.
o Every exception thrower must be one of two types:
Exception Thrower
Sample Call Sequence
try { B(); } catch (Exception e){
... }
Method A
try { C(); } catch (Exception e){
... }
Method B
try { D(); } catch (Exception e){
...
Method C
if (cond) { throw new Exception();
Method D
catcher propagator^ propagator
Stack Trace
A
B
C
D
o All types of thrown errors are instances of the Throwable class or
its subclasses.
o Serious errors are represented by instances of the Error class or
its subclasses.
o Exceptional cases that common applications should handle are
represented by instances of the Exception class or its subclasses.
Exception Types
o There are two types of exceptions:
o A checked exception is an exception that is checked at compile
time.
o All other exceptions are unchecked, or runtime, exceptions. As the
name suggests, they are detected only at runtime.
Checked vs. Runtime
o When calling a method that can throw checked exceptions
- use the try-catch statement and place the call in the try block,
or
- modify the method header to include the appropriate throws
clause.
o When calling a method that can throw runtime exceptions, it is
optional to use the try-catch statement or modify the method
header to include a throws clause.
Different Handling Rules