In this blog we are going to discuss about error and exception handling.
Note:It should be noted that only developers care about handling Exceptions although you must understand that you can handle Error using try-catch but it is useless as you cannot predict chance of occurring of an Error such as JAVA OutOfMemory.
The following diagram explains hierarchy order
Throwable is at top.
Two subclasses of it are Error and Exception.
We only deal with Exception excluding Runtime Exceptions.
Note:You can catch Error and Runtime Exceptions but it is advised not to as it is difficult to guess where and when they could occur.
To deal with Exceptions you must remember this basic rule in JAVA that Exceptions must be declared or handled.
Exceptions are of two types.
Checked Exceptions :
Include all subtypes of Exception, excluding classes that extend RuntimeException.
Checked exceptions are subject to the handle or declare rule; any method that might throw a checked exception (including methods that invoke methods that can throw a checked exception) must either declare the exception using throws, or handle the exception with an appropriate try/catch.In case of Checked Exceptions compiler strictly follows handle or declare rule.
Unchecked Exceptions :
Subtypes of Error or RuntimeException are unchecked, so the compiler doesn't enforce the handle or declare rule. You're free to handle them, or to declare them, but the compiler doesn't care one way or the other.
To deal with Exception and Error handling we use try-catch and finally blocks.
Basic definitions of try-catch and finally
try-Suspicious code is put within try block.
catch-contains code to executed for certain suspected Exceptions.
finally- code within it will be executed always whether exception occurs or not.
Sample code
import java.lang.Exception;
public class TestClass{
public static void main(String[] args)
{
try{
int d=5/0;
}
catch(Exception e) //Exception can catch any Exception but //always try be to specific
{
System.out.println(""+e.getMessage()); //This prints message if //any
}
finally{
System.out.println("Hello"); //This will run //always whether exception occured or not
}
}
}
Output will be
/ by zero
Hello
Rules for try-catch block:
1.try must be followed either by catch or finally ,or both.
2.finally will be executed always whether exception occurs or not.
3.finally can only be interrupted by System.exit(0).
4.It is legal to throw exceptions in catch or finally block.
5.If multiple Exceptions are thrown then only Exception thrown at last is reported.
Ducking (or passing the buck !)
First consider the example below :
For point 5 of try-catch and ducking consider the following example:
import java.lang.Exception;
public class tilak{
public static void main(String[] args) throws Exception //Exception is ducked till main
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi"); //this is thrown second within catch block
}
finally{
throw new Exception("Hello"); //this is thrown last within finally block and it is reported
}
}
void dos() throws Exception{
throw new Exception(""); //this is thrown first within method
}
}
Output
Exception in thread "main" java.lang.Exception: Hello
at tilak.main(tilak.java:14)
You must be wondering why in the above example,I used public static void main(String[] args) throws Exception.
This is known as ducking as it is basic rule in java that Exception must be declared or handled.When you handle it then it is OK but when you declare it you say to caller that beware ! it can cause trouble.So what caller can do is either handle the trouble or pass it's trouble to it's own caller and like this each caller will pass the trouble to it's caller(ducking) and so on until passing reaches to main if main also passes it to jvm then program will shut.
Note: throw which we used in above example can only throw Throwable and it's subclasses.
Note :for a method throwing exceptions intentionally it's return type is useless.
Consider the following example.
import java.lang.Exception;
public class tilak{
public static void main(String[] args) throws Exception
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi");
}
}
int dos() throws Exception{
return 0;
throw new Exception(""); //unreachable
}
}
OUTPUT
tilak.java:17: error: unreachable statement
throw new Exception("");
^
1 error
Consider this
import java.lang.Exception;
public class tilak{
public static void main(String[] args) throws Exception
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi");
}
}
int dos() throws Exception{
throw new Exception("");
return 0; //unreachable
}
}
OUTPUT
tilak.java:18: error: unreachable statement
return 0;
^
1 error
Note:It should be noted that only developers care about handling Exceptions although you must understand that you can handle Error using try-catch but it is useless as you cannot predict chance of occurring of an Error such as JAVA OutOfMemory.
The following diagram explains hierarchy order
Throwable is at top.
Two subclasses of it are Error and Exception.
We only deal with Exception excluding Runtime Exceptions.
Note:You can catch Error and Runtime Exceptions but it is advised not to as it is difficult to guess where and when they could occur.
To deal with Exceptions you must remember this basic rule in JAVA that Exceptions must be declared or handled.
Exceptions are of two types.
Checked Exceptions :
Include all subtypes of Exception, excluding classes that extend RuntimeException.
Checked exceptions are subject to the handle or declare rule; any method that might throw a checked exception (including methods that invoke methods that can throw a checked exception) must either declare the exception using throws, or handle the exception with an appropriate try/catch.In case of Checked Exceptions compiler strictly follows handle or declare rule.
Unchecked Exceptions :
Subtypes of Error or RuntimeException are unchecked, so the compiler doesn't enforce the handle or declare rule. You're free to handle them, or to declare them, but the compiler doesn't care one way or the other.
To deal with Exception and Error handling we use try-catch and finally blocks.
Basic definitions of try-catch and finally
try-Suspicious code is put within try block.
catch-contains code to executed for certain suspected Exceptions.
finally- code within it will be executed always whether exception occurs or not.
Sample code
import java.lang.Exception;
public class TestClass{
public static void main(String[] args)
{
try{
int d=5/0;
}
catch(Exception e) //Exception can catch any Exception but //always try be to specific
{
System.out.println(""+e.getMessage()); //This prints message if //any
}
finally{
System.out.println("Hello"); //This will run //always whether exception occured or not
}
}
}
Output will be
/ by zero
Hello
Rules for try-catch block:
1.try must be followed either by catch or finally ,or both.
2.finally will be executed always whether exception occurs or not.
3.finally can only be interrupted by System.exit(0).
4.It is legal to throw exceptions in catch or finally block.
5.If multiple Exceptions are thrown then only Exception thrown at last is reported.
Ducking (or passing the buck !)
First consider the example below :
For point 5 of try-catch and ducking consider the following example:
import java.lang.Exception;
public class tilak{
public static void main(String[] args) throws Exception //Exception is ducked till main
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi"); //this is thrown second within catch block
}
finally{
throw new Exception("Hello"); //this is thrown last within finally block and it is reported
}
}
void dos() throws Exception{
throw new Exception(""); //this is thrown first within method
}
}
Output
Exception in thread "main" java.lang.Exception: Hello
at tilak.main(tilak.java:14)
You must be wondering why in the above example,I used public static void main(String[] args) throws Exception.
This is known as ducking as it is basic rule in java that Exception must be declared or handled.When you handle it then it is OK but when you declare it you say to caller that beware ! it can cause trouble.So what caller can do is either handle the trouble or pass it's trouble to it's own caller and like this each caller will pass the trouble to it's caller(ducking) and so on until passing reaches to main if main also passes it to jvm then program will shut.
Note: throw which we used in above example can only throw Throwable and it's subclasses.
Note :for a method throwing exceptions intentionally it's return type is useless.
Consider the following example.
import java.lang.Exception;
public class tilak{
public static void main(String[] args) throws Exception
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi");
}
}
int dos() throws Exception{
return 0;
throw new Exception(""); //unreachable
}
}
OUTPUT
tilak.java:17: error: unreachable statement
throw new Exception("");
^
1 error
Consider this
import java.lang.Exception;
public class tilak{
public static void main(String[] args) throws Exception
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi");
}
}
int dos() throws Exception{
throw new Exception("");
return 0; //unreachable
}
}
OUTPUT
tilak.java:18: error: unreachable statement
return 0;
^
1 error


No comments:
Post a Comment