![]() ![]() ![]() ![]() |
|
|
|
| ![]() ![]() ![]() |
|
public void doDaIO() throws IOException { // set boolean flag if process is successful. ... if( !success ) { throw new EOFException("File error 1234"); } } |
To issue exceptions the code cannot intercept on its own (such as data in the wrong format). It must first create an instance of java.lang.Throwable, then declare the signature of a method with a throws clause with throw objects.
Subclasses can declare subclasses of the class declared. For example, EOFException (End of File Exception) is a subclass of IOException.
Among the 50+ subclasses generated by the Java runtime system's
Java Exception Classes are:
Java developers write throw objects under the Exception subclass derived from the java.lang.Thowable interface.
This Throwable class is the superclass of all errors and exceptions in the Java language.
Only objects that are instances of this class (or one of its subclasses) are (can be) thrown by the throw statement.
Similarly, only this class or one of its subclasses can be the argument type in a catch clause.
java.lang.Error subclasses (such as the VirtualMachineError subclass thrown by the JVM) are not recoverable. Errors arise when Events external to a program code occur, such as the JVM running out of memory, cause program code to stop.
All classes are checked by the compiler except for java.lang.RuntimeException and Error subclasses, which are unchecked by the compiler.
The Error subclass of the Throwable class is a sibling class of the Exception subclass. In other words, the compiler does not require coding to declare or catch Exception Errors.
try {
// The code to protect: Inventory.StockRoom srRet = srm.getStockRoom((java.lang.String)); } catch( ThrowableObjectType paramName) { // to see the message associated with the Exception: e.getMessage(); // to determine the origin of the Exception: e.printStackTrace(); // to show the Exception name and message: e.toString(); } catch( Inventory.NotAStockItem e) { // Handle "Not A Stocked Item" exception ... System.out.println("usage Advert input"); System.exit(1); } finally { // Clean-up code (close files, etc.) // regardless of whether or not an exception occured: System.out.println( "Out of catch block"); } |
For example, when trying the Thread.wait() method, the code should include a catch for InterruptException.
The JRE terminates with a NullPointerException if it can't find an
appropriate catch somewhere up the call stack.
A variable referenced in try or catch blocks must be declared before entering the try block.
Dick Baldwin has a
Tutorial on Exception Handling
David R. Nadeau's Java Exceptions Tutorial
Exception handling in German is Ausnahmebehandlung.
|
Exception | Description | Checked |
---|---|---|
java.lang.NoClassDefFoundError | the default package cannot be found in the folder list within PATH or CLASSPATH environment variable. | -- |
ArithmeticException | Arithmetic errors such as a divide by zero | NO |
ArrayIndexOutOfBoundsException | Arrays index is not within array.length | NO |
ArrayStoreException | Assignment of incompatible datatype | NO |
ClassNotFoundException | Class is not found | YES |
CloneNotSupportedException | Cloning an object that does not implement Cloneable | YES |
IllegalAccessException | No access to class | YES |
IllegalArgumentException | Illegal argument when calling a method | NO |
IllegalStateException | Illegal state for application or environment | NO |
IllegalThreadStateException | Operation not compatible with current thread | NO |
InstantiationException | Instantiating an abstract class | YES |
InterruptedException | One thread has been interrupted by another thread | YES |
NegativeArraySizeException | Creating array of negative size | NO |
NoSuchFieldException | Nonexistent field | YES |
NoSuchMethodException | Nonexistent method | YES |
NullPointerException | Invalid use of null reference | NO |
NumberFormatException | Invalid string for conversion to number | NO |
SecurityException | Security violation | NO |
StringIndexOutOfBoundsException | Index for string is out of bounds | NO |
Related:
![]()
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page ![]() Thank you! |