Seoonpageoptimization 131121033136-phpapp01-140226020828-phpapp01
233228633874018300205000-100427111019-phpapp01
-
Upload
venkatesh-boyina -
Category
Documents
-
view
214 -
download
0
Transcript of 233228633874018300205000-100427111019-phpapp01
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 1/13
Exceptions
Mr.Prasad SawantDepartment Of Computer Science
Prof .Ramkurshana More Arts, Commerce and Science College Akurdi. Mobile :9665755707Email:[email protected]
Blog:http://prasadmsawant.blogspot.com/
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 2/13
Agenda
Define exception. Discuss what is meant by exception
handing.
Describe the try,catch and finallyblocks.
Examine multiple catch blocks.
Explore nested try catch blocks. Explain the use of throw and throws
keywords.
Create user defined exception.
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 3/13
What is exception?
When an error is encountered duringthe execution of a program an
exception is said to have occurred.
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 4/13
Handling Exception
…
IF B IS ZERO GO TO EROR
C=A/B
PRINT C
GO TO EXITERROR:
DISPLAY “CODE CAUSING ERROR DUE TODIVISION BY ZERO”
EXIT:
END
BLOCKTHAT
HANDLES
ERROR
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 5/13
HANDLING EXCEPTION IN
JAVAOBJECT
THROWABLEError Exception
AWT ERROR
Thread Death..
………
SQLException
ClassNotFoundException
………
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 6/13
System-Defined Exception
Raised implicitly by system because ofillegal execution of program
When cannot continue program
execution any more Created by Java System automatically
Exception extended from Error class
and RuntimeException class
[DivByZero.java]
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 7/13
IndexOutOfBoundsException :◦ When beyond the bound of index in the object which use
index, such as array, string, and vector
ArrayStoreException :◦ When assign object of incorrect type to element of array
NegativeArraySizeException :◦ When using a negative size of array
NullPointerException : ◦ When refer to object as a null pointer
SecurityException :
◦ When violate security. Caused by security manager
IllegalMonitorStateException :◦ When the thread which is not owner of monitor involves wait
or notify method
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 8/13
Programmer-Defined Exception
Exceptions raised by programmer
Check by compiler whether the
exception handler for exception
occurred exists or not
◦ If there is no handler, it is error
Sub class of Exception class
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 9/13
The exception handling model
try
{
// code which is expected to throw an exception
}
Catch (exception e1){
/*
If exception is thrown in the try block ,is of typee1,then perform necessary action here
*/
}
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 10/13
Multiple catch block
Class catch22{
public static void main(String args[])
{
try
{
String num=args[0];
int numValue=Interger.parseInt(num);System.out.println(“The squre is ”+numValue*numvalue”);
}
catch(ArrayIndexOutOfBoundException e1)
{
System.out.println(“No argument is given!”);
}
catch(NumberFormatException e2){
System.out.println(“Not a number “);
}
}
}
C:>javac catch22
No argument is given!
C:>javac catch22 a
Not a number
C:>javac catch22 3
The squre is 9
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 11/13
Nested try-catch
… try
{
int num=args.length;
try
{
int numValue=Integer.parseInt(args[0]);System.out.println(“The squre is “+numValue*numValue);
}
catch(NumberFormatExcption e1)
{
System.out.println(“Not a Number!”);
}
}
catch (ArrayIndexOutOf BoundException e2)
{
System.out.println(“No arguments given!”);
}
….
C:>javac catch
No argument is given!
C:>javac catch22 a
Not a number
C:>javac catch22 3
The squre is 9
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 12/13
Finally block
The ‘finally ’block is guaranteed to run whether or notan exception occurs. The finally clauses of a try catchblock will always execute ,even if there are returnstatement in the try catch part .
Try block
finally Catch block
finally
No exception Exception
8/13/2019 233228633874018300205000-100427111019-phpapp01
http://slidepdf.com/reader/full/233228633874018300205000-100427111019-phpapp01 13/13
Using throws and throw
Exception are thrown with the help of the „throw‟ keyword .The „throw‟ keyword isused to indicate that an exception hasoccurred .The operand of throw is an object
of any class that is derived from the class‟ Throwable‟.
The throws keyword is used to list theexception that method can throw.