exception handling
Post on 30-Jul-2015
51 Views
Preview:
TRANSCRIPT
Exception Handling
NARAINA COLLEGE OF ENGINEERING & TECHNOLOGY
UPTU CODE-287
PRESENTED BY Priyanka
Srivastava I.T.-4th
year 7th
Semester
Contents
What is errorWhat is exception Exception handlingTry,catch n finally blockUse of throws
Java Exception Type Hierarchy
1) virtual machine errors
2)
3) everything else
What is error
An Error is any unexpected result obtained from a program during execution.
Error cann’t handle by the programmerSome 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)
Exception
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
Exception handling
Exception handling in java is managed through five keywords –
1. Try2. Catch3. Finally4. Throws5. Throw
Copyright (c) 2001 DeLorme 7
Classifying Java Exceptions
Unchecked ExceptionsIt 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 ExceptionsMust 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.
Try ,catch block
Example try {
… normal program code}catch(Exception e) {… exception handling code}
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);}}}
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.ArrayIndexOutOfBoundsException: 3Out of the block
Multiple catch block
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 }
Throw
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
handlec) 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
Throw Example
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;
Copyright (c) 2001 DeLorme 14
throw(s) Keyword
/* 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 15
Finally Keyword
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;
}
Summary
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).
top related