Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes EXCEPTIONS.   PLS HELP!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "EXCEPTIONS.   PLS HELP!!!!" Watch "EXCEPTIONS.   PLS HELP!!!!" New topic
Author

EXCEPTIONS. PLS HELP!!!!

kaushik banerjee
Ranch Hand

Joined: Mar 11, 2001
Posts: 56
Hi ,
Please see the following piece of code dealing with Exceptions which may be a bit too long 4 convenience.
public class JRS1
{
public void div()throws DivBy0Ex,XYZEx
{
int num1=10;
int num2=0;
try
{
throw new XYZEx("XYZEx thrown in method");
}

finally
{
System.out.println("Proceeding with div()");
}
System.out.println("Throwing new exception");
throw new DivBy0Ex("/ by 0");
System.out.println(num1 + "/" + num2 + "=" + (num2/num1));
System.out.println("Returning from div()");
}

public static void main(String[] a)
{
try
{
new JRS1().div();
}
catch(Exception e)
{
System.out.println("In main,dealing with " + e);
}
finally
{
System.out.println("Finally done in main");
}
}
}

class DivBy0Ex extends Exception
{
public DivBy0Ex(String s)
{
super(s);
}
}
class XYZEx extends Exception
{
public XYZEx(String s)
{
super(s);
}
}
The code when run gives the following errors :-
C:\Program Files\Xinox Software\JCreator\MyProjects\JRS1.java:19: unreachable statement
System.out.println("Throwing new exception");
^
C:\Program Files\Xinox Software\JCreator\MyProjects\JRS1.java:21: unreachable statement
System.out.println(num1 + "/" + num2 + "=" + (num2/num1));
^
2 errors
Yet a program of this type runs as desired ,ie prints
In main,dealing with java.lang.ArithmeticException: / by 0
Finally done in main
public class JRS1
{
public void div()throws Exception
{
int num1=10;
int num2=0;
if (num2==0)
throw new ArithmeticException("/ by 0");
System.out.println(num1 + "/" + num2 + "=" + (num2/num1));
System.out.println("Returning from div()");
}

public static void main(String[] a)
{
try
{
new JRS1().div();
}
catch(Exception e)
{
System.out.println("In main,dealing with " + e);
}
finally
{
System.out.println("Finally done in main");
}
}
}
Why the difference ? Can anyone pls explain ?
Thx in advance

Regards,
Kaushik
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Hello,
In the first case, once you throw an exception and do not handle it via a 'catch{}', the code will immediately return to the calling method after it calls 'finally{}'.
In the second case, the use of the 'if ()' allows an alternative path for the logic to reach the subsequent lines of code.
In short, the problem is not with exception but with the logic, here is a snippet of code that will cause the same compilation error:
void test(i) {
return; // this line prevents the logic to reach the next line
System.out.println(i);
}
but the following code snippet is Okay:
void test(i) {
if (1 == i) // this test allows a posibility for reaching lower code
return;
System.out.println(i);
}

Hope that helps
Lam
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Hello again,
My previous answer was not quite complete. In the former case, you have two 'throw' statements. One is in the try{} and one is after the finally {}. The one in try {} will call finally {} and then return to its calling method immediately. The second 'throw', after finally {}, will return immediately to the calling method. In either case the subsequent code is not reachable.
Regards,
Lam
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EXCEPTIONS. PLS HELP!!!!
 
Similar Threads
What's different between throw, throws and try catch
exception ?
Preventing finally Block from execution ...
How to identify runtime error and compiling error
what is the concept behind?