Session 7 : Java Exceptions
Java
Exceptions
Q?
What if there was no exceptions
/> If
the local error happens
while executing a method then this method causes an unconditional
program interruption.
/>
Sometimes when this error occurs, the program returns the special
value (this special values are different from null,
-1 ….), each developer
must remember the value &
meaning of this each special values while checking each error
call so this creates a tediousness in programming
Q? Ok
now we do error handling so what kind of real problems can occur
/>
code becomes messier and lengthy to
write and harder to read
Below is
the Example: Reading File
Procedure
: open file – find file size – allocate memory – read file into
memory – close file
Case
1 : Correct (but lengthy )
int
readFile(){
//open
the file;
if(operationFail)
return
-9;
//find
file size;
if(operationFail)
return
-8;
//allocate
memory
if(operationFail){
//
close file
return
-7;
}
//read
file into memory
if(operationFail){
//close
file
return
-6;
}
//close
file
if(operationFail){
return
-5;
return
0;
}
}
Case
2: Wrong (short and quick)
int
readFile(){
//open
the file;
//find
file size;
//allocate
memory
//read
file into memory
//close
file
return
0;
}
Case
3 : Best one is using the exception
int
readFile(){
try{
//open
the file;
//find
file size;
//allocate
memory
//read
file into memory
//close
file
}catch(fileOpenFailed){
//doSomething
}catch(sizeDeterminationFailed){
//doSomething
}catch(memoryAllocationFailed){
//doSomething
}catch(readFailed){
//doSomething
}catch(fileCloseFailed){
//doSomething
}
}
Q?
How error handling mechanism occurs in java
/>
The Basis Concept is “whatever
code that causes error will generate exception”
/>
Handle error by Throwing Exception and this exception is defined in
new class that is Exception Class. In Java mostly we have 3 keywords:
/>
Throw :
/>
Try:
/>
Catch:
Q?
What is the procedure of generating exception
/>
First we have to declare the
exception class
/> we
also have the method generating the
exception
/>
create an exception object
wherever you want to throw the exception
/>
use throw keyword to throw
the exception object
Q?
How do we use the throw keyword to generate exception
/>
the method interface must delcare the exception type generated within
its implementation( if more list with commas)
/> in
the case of throw the execution of current method is interrupted
immediately
/> we
can use throw in two ways:
either
exception throw occurs by method directly
or
generated by other methods
and called within the required method
ex:
public
class
Product
extends
Exception{
String
name
= "App";
public
void
Name() throws
ErrorProductName{
if(name.equals("Apple")){
Exception
e
= new
ErrorProductName();
throw
e;
}
}
}
Q?
what is interception
/> in
this mechanism of error handeling we
just run the code in first part and in the second part all the
exceptions generated in the code
portion is catched.
Ex:
try{
//first
in this part we run the code
//any
kind of exception may be generated here
}
catch(Exception
e){
//we
catch all the generated exception here
//we
do the error handling in this part
}
Q?
how does the execution flow occurs in the Exception generation
/>
try{
method1();
method2();
method3();
}
catch(Exception
e){
System.out.println("Error");
}
Two
cases can occur:
1.
if neither of the method generate exception then all the method
executes
2.
if method1() generates exception while method2() and method3()
doesnot then
method2()
and method3() are skipped.
Note:
we can also perform multiple catch by using seperate
catch block: whichever method fails the respective catch is
only printed out.
try{
method1();
method2();
method3();
}
catch(Exception
e){
System.out.println("Error");
}
catch(IOException
oe){
System.out.println("IO
Error");
}
Q?
What is exceptions hierarchy
/>
The superclass Throwable is
subclassed to Error and
Exception
/>
Error contains : internal
error, hard failure in VM (OutOfMemoryError) : this errors are
unchecked
/>
Exceptions : programming
errors which contains RuntimeException (NullPointerException) this
are unchecked
where as
checked exceptions like : ClassNotFoundException,
NoSuchMethodException
Note* :
unchecked exception are
those which can be thrown ”at any time ” , the caller do not have
to handle them explicitly
whereas
checked exception are those
which we have to manually declare and check them
Note* :
the keyword finally is used
to specify the actions that must be always executed
try{
....
}
finally{
end();
}
Q?
How do we handle exception in case of loop
/>
In case of single iteration, we use the try-catch block inside the
loop. In case if the exception occurs then the execution goes to the
catch block and then proceed with the next iteration.
while(condition){
try{
//code
here
}catch(Exception
e){
//handle
exception here
}
}
But we
can also check the whole loop and try to catch the exception occurred
in a block of loop as well:
try{
while(condition){
//code
here
}
}catch(Exception
e){
//handle
exception here
}
Q?
What are the precautions for testing exception
/>
Two important cases:
1>
Expected exception test : we
expect an anomaly and so we should throw exception:
=>
in this case the test can fail , if No Exception is detected
2>
Unexpected exception test:
we expect normal behaviour so no exception should be raised:
=> in
this case the test will fail, if that Exception is raised
Q?
when to choose between checked and unchecked exception
/>
Rule
for Checked : think of unchecked exception as a testable
conditions before the code executes
ex:
x.doSomething();//
this code throws NullPointerException
if
(x==null)
{
/*do
something below to make sure when x.doSomething() is executed, it
//won’t throw a NullPointerException
*/
x
= new
X();
}
x.doSomething();
Rule
for Unchecked : think of unchecked exception as an un-testable
conditions that may occur while the code executes.
Socket
s = new
Socket(“google.com”,
80);// the
DNS server
can
come down anytime
Comments
Post a Comment