Kcs-602 Lecture05 Exceptions Cse
Kcs-602 Lecture05 Exceptions Cse
Exception Definition
Exception Occurrence
Exception Handling
Exception Propagation
Exception
• Error occurred in execution time.
• Abnormal termination of program.
• Wrong execution result.
• Java provides an exception handling mechanism in
its language system. Benefits..
– Improve the reliability of application program
– Allow simple program code for exception check and
handling into source
Hierarchical Structure of
Throwable Class
Object
Throwable
Error Exception
... RuntimeException
...
...
Java’s build in Exceptions
ClassNotFoundException
IOException
ArithmeticException
Exception AWTException
NullPointerException
RuntimeException
IndexOutOfBoundsException
Object Throwable Several more classes
IllegalArgumentException
VirtualMachineError
Error
AWTError
java.lang.NullPointerException
at ThrowStatement.exp(ThrowStatement.java:4)
at ThrowStatement.main(ThrowStatement.java:8)
throws
• A method is capable of throwing an exception which is
does not handles.
• throws clause lists the type of exception that a given
method is capable of throwing..except Error and
RuntimeException and their subclasses.
• Type method_name (parameter-list) throws exception-list
{//body of method}
• These exception may not be handled in the causing method
but it will have to be handled by the calling class or
method.
Exception Occurrence
• throws Statement
– When programmer-defined exception is raised,
if there is no exception handler, need to
describe it in the declaration part of method
[modifiers] returntype methodName(params) throws e1, ... ,ek { }
All other exceptions that a method can throw must be declared in the throws clause.If
they are not, a compile-time error will result.
That should be handle by try..catch.. (during call/use )
class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
// finally
}
}
inside throwOne
caught java.lang.IllegalAccessException:
demo
Exception Handling
• try-catch-finally Statement
– Check and Handle the Exception
try {
// …
} catch (ExceptionType1 identifier) {
// …
} catch (ExceptionType2 identifier) {
// …
} finally {
// …
}
// Demonstrate finally.
class FinallyDemo {
// Throw an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's
finally");
}
}
// Return from within a try block.
static void procB() {
try {
System.out.println("inside procB");
return;
} finally {
System.out.println("procB's
finally");
}
}
// Execute a try block normally.
static void procC() {
try {
System.out.println("inside
procC");
} finally {
System.out.println("procC's
finally");
}
}
public static void main(String args[]) {
try {
procA();
} catch (Exception e) {
System.out.println("Exception
caught");
}
procB();
procC();
}
}
Café Example
abstract class TemperatureException extends Exception {
private int temperature; // in Celsius
public TemperatureException() {
System.out.println("Temperature exception occurred");
}
TemperatureException
TooColdException TooHotException
class person{
void drinkCofee (int temperature) throws TooColdException, TooHotException
{
if (temperature <= 65) {
throw new TooColdException();
}
else if (temperature >= 85) {
throw new TooHotException();
}
}
}
class cafe {
public static void main (String [] args)
{
}}
Coffee tasted fine
70 Person Left
throw keyword is used within the throws keyword is used with the
method. method signature.