exception handling
TRANSCRIPT
![Page 1: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/1.jpg)
Exception Handling
NARAINA COLLEGE OF ENGINEERING & TECHNOLOGY
UPTU CODE-287
PRESENTED BY Priyanka
Srivastava I.T.-4th
year 7th
Semester
![Page 2: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/2.jpg)
Contents
What is errorWhat is exception Exception handlingTry,catch n finally blockUse of throws
![Page 3: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/3.jpg)
Java Exception Type Hierarchy
1) virtual machine errors
2)
3) everything else
![Page 4: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/4.jpg)
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)
![Page 5: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/5.jpg)
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
![Page 6: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/6.jpg)
Exception handling
Exception handling in java is managed through five keywords –
1. Try2. Catch3. Finally4. Throws5. Throw
![Page 7: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/7.jpg)
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.
![Page 8: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/8.jpg)
Try ,catch block
Example try {
… normal program code}catch(Exception e) {… exception handling code}
![Page 9: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/9.jpg)
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);}}}
![Page 10: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/10.jpg)
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
![Page 11: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/11.jpg)
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 }
![Page 12: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/12.jpg)
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
![Page 13: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/13.jpg)
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;
![Page 14: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/14.jpg)
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. */
}
![Page 15: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/15.jpg)
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;
}
![Page 16: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/16.jpg)
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).
![Page 17: Exception handling](https://reader035.vdocuments.site/reader035/viewer/2022080908/55ba462cbb61eb75438b4668/html5/thumbnails/17.jpg)