This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptional Handling Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptional Handling Question" Watch "Exceptional Handling Question" New topic
Author

Exceptional Handling Question

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi all,

Can someone tell me why if I have line 1, the System.out.println statement is not showing any compilation error, but if I take it off and replace with throw new ArithmeticException();, then it's showing a compilation error? It says that the print statement in the try block is unreachable? Both are basically doing the same thing right, throwing an ArithmeticException?


Thanks.
[ September 28, 2008: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
No, they are different
Case 1
public class ExceptionTest {


public static void main(String args[])
{
try
{
System.out.println(2/0); //Line 1 throws an ArithmeticException. Compiler dont know for sure if an exception can occur as code in the try block may cause an exception. The next println is not executed and control runs to the nearest catch that matches and the finally is executed. It doesnt show any compiler error because you are handling you error. Even if you dont handle it wont show compiler error but an runtime exception will occur.
System.out.println("try block");
}

catch(Exception e)
{
System.out.println("catch block");
}

finally
{
System.out.println("finally block");
}

}

}

Case 2
public class ExceptionTest {


public static void main(String args[])
{
try
{
throws new ArithmeticException(); // here you are throwing an exception object so surely the next line wont be executed so compiler error as it become unreachable.
System.out.println("try block");
}

catch(Exception e)
{
System.out.println("catch block");
}

finally
{
System.out.println("finally block");
}

}

}
[ September 28, 2008: Message edited by: vidhya suvarna ]

SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
Yes, vidhya is right, compiler only checks for correct syntax and it is never bothered about user defined code.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Vidhya,

I thought for case 2 also, the nearest catch/finally will catch the thrown exception. Why will it not?
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Yeas Rekha, the exception will be passed to nearest catch/finally. Only difference is that System.out.println("try block"); will not be executed for sure.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Vidhya,
My question was similar to Arjun's question.

When we do an explicit throw new ArithmeticException();, why does not the nearest catch block catch it? Does the print statement after the line causing the exception matter?

In other words, there is a line throwing an exception. And, there is a catch block to catch it. So, irrespective of what is there after the line throwing the exception, the catch block should execute right? Why do we get a compiler error? (I can understand that the statement printing "try block" is unreachable, but what is causing the compiler error?)

I hope I am clear in my question.
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Rekha,
When you are explicitly throwing an exception using a THROW clause, it simply means that an exception has occured. Also it means that lines following the THROW will never be reached as the exception will be moved to catch clause.
In java, unreachable code is a compiler error. So thats why you get an compiler error.

Hope this explaination is helpful.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Yup I got it Vidhya !! Thanks...
 
jQuery in Action, 2nd edition
 
subject: Exceptional Handling Question
 
Similar Threads
return statement effect in Exception class
No Unreachable error ?
Exception Handling!!!
A runtime exception is throws in the catch clause:
need guidance