File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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.
 
 
subject: Throwing Exceptions