4. An Error is any unexpected result obtained
from a program during execution.
Error cann’t handle by the programmer
Some typical causes of errors:
Memory errors (i.e. memory incorrectly
allocated, memory leaks, “null pointer”)
File system errors (i.e. disk is full, disk has
been removed)
Network errors (i.e. network is down)
5. It is the problem that arise during the
exection of a program.
The exception class is as subclass of
throwable class.
Other than the exception class there is
another sub class which is derived from
throwable class
6. Exception handling in java is managed
through five keywords –
1. Try
2. Catch
3. Finally
4. Throws
5. Throw
7. Unchecked Exceptions
It is not required that these
types of exceptions be caught or
declared on a method.
Runtime exceptions can be
generated by methods or by
the JVM itself.
Errors are generated from
deep within the JVM, and
often indicate a truly fatal
state.
Runtime exceptions are a
source of major controversy!
Checked Exceptions
Must either be caught by a
method or declared in its
signature.
Placing exceptions in the
method signature harkens
back to a major concern for
Goodenough.
This requirement is viewed
with derision in the hardcore
C++ community.
A common technique for
simplifying checked
exceptions is subsumption.
Copyright (c) 2001 DeLorme 7
8. Example
try {
… normal program code
}
catch(Exception e) {
… exception handling code
}
9. import java.io.*;
public class demo
{
public static void main(String[] args)
{
try
{
int a,b,c;
a=5;
b=0;
c=a/b;
System.out.println(c);
}
catch(ArithmeticException ae)
{
System.out.println("hello");
System.out.println(ae);
}
}}
10. import java.io.*;
public class ExcepTest{
public static void main(String args[]){
try{
int a[] = new int[2];
System.out.println("Access element
three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException
e){
System.out.println("Exception thrown
:" + e);
}
System.out.println("Out of the block");
}
}
This would produce following result:
Exception thrown
:java.lang.ArrayIndexOutOfBoundsExceptio
n: 3
Out of the block
11. A try block can be followed by multiple catch blocks. The syntax for
multiple catch blocks looks like the following:
try
{
//Protected code
}catch(ExceptionType1 e1)
{
//Catch block
}catch(ExceptionType2 e2)
{
//Catch block
}catch(ExceptionType3 e3)
{
//Catch block
}
12. When an error condition is detected, it is necessary to handle it immediately where
detected or throw the exception to the calling method.
Exception handling is a mechanism for transferring control from where an error
occurred to where it can be handled most appropriately.
After the exception is thrown, the throwing method terminates and execution
control is immediately passed backward along the chain of callers from where the
exception was thrown.
Any method along the calling chain can:
a) handle the exception and continue with execution,
b) handle the exception and again throw the exception to the calling method to
handle
c) or do nothing, terminate immediately and let the calling method deal with the
exception.
The down method below is an example of throwing an exception, in this case when
the counter n becomes
12
13. 13
public int down() throws counterException {
if (n <= 0)
throw new counterException(
n + " count Down failed.");
return --n;
}
•The down method is an example of throwing an exception, in this case when the
counter n becomes negative.
•If exception is thrown, execution does not reach return --n;
14. /* The IllegalArgumentException is considered unchecked, and
* even making it part of the signature will not alter that. */
public void setName(String p_strName) throws IllegalArgumentException
{
/* valid names cannot be zero length */
if (p_strName.length() == 0) {
throw new IllegalArgumentException(“…”);
}
m_strName = p_strName;
}
public void foo() {
setName(“”); /* No warning about unhandled exceptions. */
}
Copyright (c) 2001 DeLorme 14
15. public bool anotherMethod(Object myParameter) {
try { /* What value does this snippet return? */
myClass.myMethod(myParameter);
return true;
} catch (Exception e) {
System.err.println(“Exception in anotherMethod() “+e.toString());
return false;
} finally {
/* If the close operation can raise an exception, whoops! */
if (myClass.close() == false) {
break;
}
}
return false;
}
Copyright (c) 2001 DeLorme 15
16. Exceptions are a powerful error handling
mechanism.
Exceptions in Java are built into the language.
Exceptions can be handled by the
programmer (try-catch), or handled by the
Java environment (throws).