introduction to exception
TRANSCRIPT
![Page 1: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/1.jpg)
EXCEPTIONS
-By SWAPNIL S. MHADLEKAR
3-Dec-15 1
![Page 2: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/2.jpg)
WHAT IS AN EXCEPTION???
EXCEPTION IS AN ABNORMAL CONDITION WHICH INTERRUPTS THE SMOOTH EXECUTION OF PROGRAM.
E.g.
1)If you are trying to access 6th element in the array:
a[1,2,3,4,5]
a[5]=9;// throw ArrayIndexOutOfBound Exception
2) trying to divide a number by zero
int a =5, b=0;
int result= a/0; //throw Arithmetic Exception
![Page 3: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/3.jpg)
EXCEPTION vs ERRORS
Errors are thrown by the runtime system which indicate some conditions during the program execution.
When the error is thrown by the system, application cannot recover it and the program execution halts.
Examples.
Java.lang.OutOfMemoryError
Java.lang.StackOverflowError
![Page 4: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/4.jpg)
EXCEPTIONS HIERARCHY
java.lang.Objectjava.lang.Throwable
![Page 5: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/5.jpg)
CHECKED vs UNCHECKED EXCEPTIONS
Checked Exception-
Any exception that extends java.lang.Exceptionclass is called a “checked exception”.
A checked exception must be handled using try and catch blocks or the method should declare them in throws.
![Page 6: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/6.jpg)
Unchecked Exception
Any exception that extends java.lang.RunTimeException is called “Unchecked Exception”.
They are also called “RunTime Exceptions”.
There is no need to handle the unchecked exception and also to declare using throws.
![Page 7: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/7.jpg)
RULES TO FOLLOW:
A try block must be followed by a catch or finally or both.
There should not be any statements between try and catch and finally.
Finally is optional and there should be only one finally block.
There can be multiple catch blocks.
The narrow exceptions must be declared first and the broader exception last.
![Page 8: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/8.jpg)
EXCEPTION HANDLING
Exceptions are recoverable. So we need to handle them.
We handle the exception just to ensure that the program is not terminated.
Keywords-
try
catch
throws
throw
finally
![Page 9: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/9.jpg)
So How To Deal With Exception??
1) The Method can contain the code for the handler itself:
![Page 10: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/10.jpg)
2) The Method can alert other methods about the exception it throws. (Duck)
![Page 11: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/11.jpg)
Syntax:
try{//risky code goes hereUsing whatsApp during lecture}
catch(Exception ex){//Recovery code goes hereApologize to teacher}
finally{// part of code which must execute independent of the
exceptionLecture over}
![Page 12: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/12.jpg)
throw And throws:
When we say that an exception is thrown, it means that an object of Exception class is created.
It is created by the method where the exception occurred.
This object is then handed over to the Runtime system.
The Runtime system then looks for the code that can handle it.
This code is called “”Exception Handler.
![Page 13: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/13.jpg)
Exception Handler:
![Page 14: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/14.jpg)
MY CUSTOM EXCEPTION
We can create our own custom exception by extending java.lang.Exception class.
E.g.
public class MyCustomException
extends Exception{
}
Or
public class MyCustomException
extends RunTimeException{
}
![Page 15: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/15.jpg)
EXCEPTION PROPAGATION
Method implementation takes place in stack.
![Page 16: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/16.jpg)
Propagation:
![Page 17: Introduction to Exception](https://reader031.vdocuments.us/reader031/viewer/2022030214/588ac10c1a28ab371f8b690d/html5/thumbnails/17.jpg)
3-Dec-15 17
THANK YOU !!