Throwable
All exceptions descend from the Throwable class, which extends Object.
- Only objects that descend from
Throwablecan be thrown and caught.
A Throwable encapsulates a String message, and provides methods like getMessage and printStackTrace.
Throwabledefines a constructor that takes aStringfor the message, as do almost all descendant classes.
Java defines three distinct categories of Throwables:
* Exception
* RuntimeException
* Error
Exception¶
Exception extends Throwable and is the ancestor of all checked exceptions.
Packages define application-specific Exception subclasses and descendants whose names describe the condition causing the exception to be thrown.
public class IOException extends Exception { /*...*/ }
public class FileNotFoundException extends IOException { /*...*/ }
public class FileReader extends InputStreamReader {
public FileReader(String fileName) throws FileNotFoundException { /*...*/ }
//...
}
-
By convention, names of checked exceptions end in
Exception. -
Subclasses can add new, application-specific fields and methods if they choose.
remember...¶
Checked exceptions represent conditions external to a program that well-written code can anticipate and recover from.
RuntimeException¶
RuntimeException extends Exception, but is special: it and its subclasses are unchecked exceptions.
public class RuntimeException extends Exception { /*...*/ }
class IllegalArgumentException extends RuntimeException { /*...*/ }
public class SecurityException extends RuntimeException { /*...*/ }
Callers of methods that throw these exceptions can, but are not required to, catch or declare them.
SecurityManager sm = new SecurityManager();
sm.checkListen(80); // throws SecurityException, don't have to catch.
try {
sm.checkRead("/etc/sudoers"); // throws SecurityException, can catch if we want.
}
catch (SecurityException e) {
System.out.println(e);
}
remember...¶
Unchecked exceptions typically represent conditions internal to a program that well-written code can anticipate and either prevent or recover from.
Error¶
Error extends Throwable, so is a sibling (not descendant of) Exception.
public class Error extends Throwable { /*...*/ }
- By convention, error class names end in
Error, notException.
Errors are also unchecked by the compiler - callers of methods that may throw Error do not have to catch or declare them.
- This is because even the most well-written program is not expected to recover from an
Errorand continue executing.
Most errors are generated by the JVM itself.
abstract public class VirtualMachineError extends Error { /*...*/ }
public class StackOverflowError extends VirtualMachineError { /*...*/ }
Errors represent conditions from which recovery is likely impossible.
-
These include conditions like running out of memory, overflowing the method call stack, trying to run a program with no
main, or even a bug in the code of the JVM. -
You don't write
try/catchblocks forErrors.
remember...¶
Errors represent conditions from which recovery is likely impossible. You should not attempt to handle them.
¶
Drill¶
Exceptions/com.example.exceptions.drills.ErrorDrill* Open ErrorDrill.java and add code to thecallMemethod as the comments describe. * What happens when you runErrorDrill? * Right-click on the Console view and select Preferences. Un-check Limit console output and runErrorDrillagain.