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 Throwing Exceptions 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 "Throwing Exceptions" Watch "Throwing Exceptions" New topic
Author

Throwing Exceptions

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
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

Joined: Sep 18, 2000
Posts: 208
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


Regards,<BR>Shree
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing Exceptions
 
Similar Threads
Anonymous class behavior confusion !
please help me
java.lang.NoSuchMethodError: main - What am I doing wrong?
what is wrong with the code??
4 questions from valiveru mock test