Professional Documents
Culture Documents
Exception Handling: - Exception Definition - Exception Occurrence - Exception Handling - Exception Propagation
Exception Handling: - Exception Definition - Exception Occurrence - Exception Handling - Exception Propagation
•Exception Definition
•Exception Occurrence
•Exception Handling
•Exception Propagation
Exception Definition
• Treat exception as an object
• All exceptions are instances of a class
extended from Throwable class or its
subclass.
Throwable
Throwable
Error
Error Exception
Exception
... RuntimeException
RuntimeException
...
...
Definition of Exception
• Error Class
– Critical error which is not acceptable in normal
application program
• Exception Class
– Possible exception in normal application
program execution
– Possible to handle by programmer
System-Defined Exception
• Raised implicitly by system because of
illegal execution of program
• When cannot continue program execution
any more
• Created by Java System automatically
• Exception extended from Error class and
RuntimeException class
System-Defined Exception
• IndexOutOfBoundsException :
– When beyond the bound of index in the object which use index, such as
array, string, and vector
• ArrayStoreException :
– When assign object of incorrect type to element of array
• NegativeArraySizeException :
– When using a negative size of array
• NullPointerException :
– When refer to object as a null pointer
• SecurityException :
– When violate security. Caused by security manager
• IllegalMonitorStateException :
– When the thread which is not owner of monitor involves wait or notify
method
Programming in Java
Example 1 :-
public class Program1 {
This limits us to only a few values and we are unable to prompt the user.
The usual way of getting input is to issue a prompt and then receive data from
the keyboard within the program as follows :-
import java.io.*;
public class FileWriteDemo {
Exceptions are of different types and form a class hierarchy, just as other
classes do.
try {
myGarage.addVehicle(new Car(“ABC123”,4));
} //end try
catch(GarageFull gf){
System.err.println(“Operation failed”);
System.err.println(“Capacity is “ + gf.getCapacity());
} //end catch
Programming in Java - Exception handling
Exception classes fall into two groups - sub-classes of RunTimeException
and others. In the case of others :-
InterruptedException IOException
IOException RunTimeException ParseException
InterruptedException RunTimeException ParseException
IndexOutOfBoundsException
IndexOutOfBoundsException ArithmeticException
ArithmeticException NullPointerException
NullPointerException ArithmeticException
ArithmeticException
Programming in Java - Exception handling
public String getMessage() { //handles exception internally
String msg;
boolean ok = false;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
System.out.println(“Enter message “);
while (!ok){
try {
msg = in.readLine();
ok = true;
} //end try
catch(IOException ioe){
System.err.println(“Try again”);
}//end catch
}//end while public String getMessage() throws IOException { //does not
return msg; String msg;
} //end GetMessage InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
System.out.println(“Enter message “);
msg = in.readLine();
return msg;
} //end GetMessage