This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Throwing Exceptions

 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class MyClass
{
int i;
public void amethod()
{
try
{
i=10;
i++;
throw new ArithmeticException();
i--;//stmt not reached --- compilation er error.
}
catch(Exception e){i++;}
finally{i--;
System.out.println("i is:"+i);}
}
public static void main(String args[])
{
MyClass mc = new MyClass();
mc.amethod();
}
}
Is the compliation error "Statement not reached" because an exception is explicitly thrown ,following which there should'nt be any code ?
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It is preferable if you check some condition and then throw the Arithmetic Exception error. In this case, the exception will be thrown in all cases and the statement mentioned can never be executed. The compiler senses this.

------------------
Regards,
Shree
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For every statement in your code, there must be at least one execution path that reaches it. If no such path exists, then the compiler considers it unreachable code, which is a compile-time error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic