Day 7
DAY 7
JAVA
Exception
An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.
Error vs Exception
Error: An Error indicates serious problem that a reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.
Exception Terminates Java Program
package
com.myjava.exceptions;
public
class
MyException {
public
static
void
main(String a[]){
for
(
int
i=
5
;i>=
0
;i--){
System.out.println(
10
/i);
}
System.out.println(
"After for loop..."
);
}
}
Java Exception Handling Sample Code
package
com.myjava.exceptions;
public
class
MyExceptionHandle {
public
static
void
main(String a[]){
try
{
for
(
int
i=
5
;i>=
0
;i--){
System.out.println(
10
/i);
}
}
catch
(Exception ex){
System.out.println(
"Exception Message: "
+ex.getMessage());
ex.printStackTrace();
}
System.out.println(
"After for loop..."
);
}
}
Exception Hierarchy
All exception and errors types are sub classes of class Throwable, which is base class of hierarchy.One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception.Another branch,Error are used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). StackOverflowError is an example of such an error.
How JVM handle an Exception
Default Exception Handling : Whenever inside a method, if an exception has occurred, the method creates an Object known as Exception Object and hands it off to the run-time system(JVM). The exception object contains name and description of the exception, and current state of the program where exception has occurred. Creating the Exception Object and handling it to the run-time system is called throwing an Exception.There might be the list of the methods that had been called to get to the method where exception was occurred. This ordered list of the methods is called Call Stack.Now the following procedure will happen.
- The run-time system searches the call stack to find the method that contains block of code that can handle the occurred exception. The block of the code is called Exception handler.
- The run-time system starts searching from the method in which exception occurred, proceeds through call stack in the reverse order in which methods were called.
- If it finds appropriate handler then it passes the occurred exception to it. Appropriate handler means the type of the exception object thrown matches the type of the exception object it can handle.
- If run-time system searches all the methods on call stack and couldn’t have found the appropriate handler then run-time system handover the Exception Object to default exception handler , which is part of run-time system. This handler prints the exception information in the following format and terminates program abnormally.
How Programmer handles an exception?
Customized Exception Handling : Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Briefly, here is how they work. Program statements that you think can raise exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch block) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed after a try block completes is put in a finally block.
THROWS CLAUSE
package
com.myjava.exceptions;
public
class
MyThrowsClause {
public
static
void
main(String a[]){
MyThrowsClause mytc =
new
MyThrowsClause();
try
{
for
(
int
i=
0
; i<
5
; i++){
mytc.getJunk();
System.out.println(i);
}
}
catch
(InterruptedException iex){
iex.printStackTrace();
}
}
public
void
getJunk()
throws
InterruptedException {
Thread.sleep(
1000
);
}
}
Multiple Cache Blocks Sample Code
package
com.myjava.exceptions;
import
java.net.MalformedURLException;
import
java.net.URL;
public
class
MyMultipleCatchBlocks {
public
static
void
main(String a[]){
MyMultipleCatchBlocks mmcb =
new
MyMultipleCatchBlocks();
mmcb.execute(
1
);
mmcb.execute(
2
);
}
public
void
execute(
int
i){
try
{
if
(i ==
1
){
getIntValue(
"7u"
);
}
else
{
getUrlObj(
"www.junksite.com"
);
}
}
catch
(NumberFormatException nfe){
System.out.println(
"Inside NumberFormatException... "
+nfe.getMessage());
}
catch
(MalformedURLException mue){
System.out.println(
"Inside MalformedURLException... "
+mue.getMessage());
}
catch
(Exception ex){
System.out.println(
"Inside Exception... "
+ex.getMessage());
}
}
public
int
getIntValue(String num){
return
Integer.parseInt(num);
}
public
URL getUrlObj(String urlStr)
throws
MalformedURLException{
return
new
URL(urlStr);
}
}
Finally Block Sample Code
package
com.myjava.exceptions;
public
class
MyFinallyBlock {
public
static
void
main(String[] a){
/**
* Exception will occur here, after catch block
* the contol will goto finally block.
*/
try
{
int
i =
10
/
0
;
}
catch
(Exception ex){
System.out.println(
"Inside 1st catch Block"
);
}
finally
{
System.out.println(
"Inside 1st finally block"
);
}
/**
* In this case exception won't, after executing try block
* the contol will goto finally block.
*/
try
{
int
i =
10
/
10
;
}
catch
(Exception ex){
System.out.println(
"Inside 2nd catch Block"
);
}
finally
{
System.out.println(
"Inside 2nd finally block"
);
}
}
}
- In a method, there can be more than one statements that might throw exception, So put all these statements within its own try block and provide separate exception handler within own catch block for each of them.
- If an exception occurs within the try block, that exception is handled by the exception handler associated with it. To associate exception handler, we must put catch block after it. There can be more than one exception handlers. Each catch block is a exception handler that handles the exception of the type indicated by its argument. The argument, ExceptionType declares the type of the exception that it can handle and must be the name of the class that inherits from Throwable class.
- For each try block there can be zero or more catch blocks, but only one finally block.
- The finally block is optional.It always gets executed whether an exception occurred in try block or not . If exception occurs, then it will be executed after try and catch blocks. And if exception does not occur then it will be executed after the try block. The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection.
Comments
Post a Comment