Difference between revisions of "Exceptions"
From Suhrid.net Wiki
Jump to navigationJump to search(3 intermediate revisions by the same user not shown) | |||
Line 23: | Line 23: | ||
* Each method must follow the handle or declare law for checked exceptions. | * Each method must follow the handle or declare law for checked exceptions. | ||
− | * The compiler will check if the code within the try block can throw | + | * The compiler will check if the code within the try block can throw any checked exceptions in the catch block, if not it will flag an error. See below: |
+ | ** This is '''true only for checked exceptions'''. The compiler wont complain for catch clauses for Exception, Error and Throwable. | ||
<syntaxhighlight lang="java5"> | <syntaxhighlight lang="java5"> | ||
Line 65: | Line 66: | ||
+ | * Although a finally block is always executed, it cannot "handle" exceptions, a catch block '''must be present'''. | ||
+ | |||
+ | <syntaxhighlight lang="java5"> | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | try { | ||
+ | foo(); //ILLEGAL, CATCH BLOCK MUST BE PRESENT | ||
+ | } finally { | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | private static void foo() throws IOException { | ||
+ | |||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | == finally block == | ||
+ | |||
+ | * The finally block is always called, no matter what. (Other than a System.exit() ! ) | ||
+ | |||
+ | <syntaxhighlight lang="java5"> | ||
+ | |||
+ | import java.io.IOException; | ||
+ | |||
+ | public class Finale { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | try { | ||
+ | foo(); | ||
+ | } catch (Exception e) { | ||
+ | System.out.println("EEE"); | ||
+ | return; | ||
+ | } finally { | ||
+ | System.out.println("Finale"); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private static void foo() throws IOException { | ||
+ | throw new IOException(); | ||
+ | } | ||
+ | |||
+ | /* Output: | ||
+ | * | ||
+ | * EEE | ||
+ | * FINALE | ||
+ | */ | ||
+ | |||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
[[Category:OCPJP]] | [[Category:OCPJP]] |
Latest revision as of 03:45, 7 September 2011
Introduction
- Exceptions are a mechanism to detect and handle errors without writing special code to test return values.
- Error handling code for common errors can be handled in a single place.
Structure
- A try clause has to have either a catch clause or a finally clause or both.
- catch must follow try.
- finally must follow the last catch clause.
Exception Hierarchy
- All exception classes are subtypes of java.lang.Exception.
- RunTimeException is a subtype of Exception which represents unchecked exceptions. All unchecked exception will inherit from this class.
- Classes which derive from java.lang.Error indicate serious problems that an application should not try to catch. e.g OutOfMemory error.
- Both Error and Exception classes are derived from the java.lang.Throwable class.
Exception Handling
- The order of catch clauses is important.
- If the exceptions are in the same class hierarchy, The most-specific exception type should be first, followed by the more general exceptions. Otherwise, the code will not compile.
- If exceptions are in different class hierarchy, then the order does not matter.
- Each method must follow the handle or declare law for checked exceptions.
- The compiler will check if the code within the try block can throw any checked exceptions in the catch block, if not it will flag an error. See below:
- This is true only for checked exceptions. The compiler wont complain for catch clauses for Exception, Error and Throwable.
public static void main(String[] args) {
try {
f();
} catch(FileNotFoundException f) {
} catch(InterruptedException e) { //This will be a compiler error, since f() can never throw InterruptedException
}
}
private static void f() throws FileNotFoundException {
}
- Any exceptions thrown from within a catch block will not be handled by the other catch clauses of the main try block.
- These exceptions have to be handled or declared separately.
public static void main(String[] args) {
try {
f();
} catch(FileNotFoundException f) {
throw new IOException(); //The below catch block will not handle this IOException. It has to be caught here or the method has to declare that it throws IOException.
} catch(IOException e) {
}
}
private static void f() throws FileNotFoundException {
}
- Although a finally block is always executed, it cannot "handle" exceptions, a catch block must be present.
public static void main(String[] args) {
try {
foo(); //ILLEGAL, CATCH BLOCK MUST BE PRESENT
} finally {
}
}
private static void foo() throws IOException {
}
finally block
- The finally block is always called, no matter what. (Other than a System.exit() ! )
import java.io.IOException;
public class Finale {
public static void main(String[] args) {
try {
foo();
} catch (Exception e) {
System.out.println("EEE");
return;
} finally {
System.out.println("Finale");
}
}
private static void foo() throws IOException {
throw new IOException();
}
/* Output:
*
* EEE
* FINALE
*/
}