Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptional Handling Question

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, vidhya is right, compiler only checks for correct syntax and it is never bothered about user defined code.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidhya,

I thought for case 2 also, the nearest catch/finally will catch the thrown exception. Why will it not?
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup I got it Vidhya !! Thanks...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic