Professional Documents
Culture Documents
– Runtime Errors – arise because the program tries to perform an operation that is
impossible to carry out.
– Logic Errors – arise because the program does perform the way it was intended to.
Intro to Exceptions • Syntax errors are caught by the compiler, and fixed before the
program is run.
• An error is also represented as an object in Java, but • What if Exception is not handled?
usually represents an unrecoverable situation and should – Might terminate program execution
not be caught
1
Checked Exceptions
Portion of
Throwable Throwable UnChecked Exceptions
hierarchy Error
Exception Error
Runtime
IOException AWTError ThreadDeath
OutOf How are
Exception MemoryError
Java Exceptions Handled
ArrayIndexOutOfBoundsException InputMismatchException
• try block
• throws
– Code that could generate errors put in try blocks
– throws exception out of the method, requiring it to be caught and
• catch block handled by an appropriate exception handler
– Code for error handling enclosed in a catch clause – Any exception that is thrown out of a method must be specified as
such by a throws clause.
• finally block
– The finally clause always executes
– Resources that are opened may need to be closed during
exception handling
– Appears after the last catch block
– It wil not execute if System.exit(0) occurs first
//write code for exception handling Execute catch block Execute catch block Execute catch block
…..
} for for for
exception 1 exception 2 exception n
……
catch (<exception to be caught>) //catch block
{
//code for exception handling
} finally //finallyblock Execute finally block
{
//any clean-up code, release the acquired resources
}
2
Example: Unchecked Exceptions
Examples }
System.out.println(args[0]);
}
}
3
Example: Checked Exceptions
Example: Checked Exceptions compile & execute
import java.io.*;
String s = br.readLine();
System.out.println(s);
}//end of main
}//end of class
4
Multiple Catch Blocks Example: Multiple catch blocks
/* numbers.txt contains numbers. After reading number
• Possible to have multiple catch clauses for a from file, prints its square on console */
single try statement
[
import java.io.*;
– Essentially checking for different types of exceptions public class MCatchDemo {
that may happen public static void main(String args[ ]) {
try {
//may throw FileNotFound & IOException
• Evaluated in the order of the code FileReader fr = new FileReader (“numbers.txt”);
BufferedReader br = new BufferedReader(fr );
– Bear in mind the Exception hierarchy when writing
//read the line
multiple catch clauses!
String s = br.readLine();
– If you catch Exception first and then IOException, the
//may throw NumberFormatException, if s is not no.
IOException will never be caught!
int number = Integer.parseInt(s);
System.out.println(number * number);
5
printStackTrace() is your friend! What if
• When dealing with exceptions – Method throws back the exception
• Especially when debugging – No catch blocks matches
• printStackTrace() will: – Exception is not handled
– Show you the full calling history
calls main
– With line numbers calls method1 calls method2
6
Compile & Execute
If “string.txt” isn’t there, it will throw FileNotFoundException