File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Throwing Exceptions

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
public class MyClass
int i;
public void amethod()
throw new ArithmeticException();
i--;//stmt not reached --- compilation er error.
catch(Exception e){i++;}
System.out.println("i is:"+i);}
public static void main(String args[])
MyClass mc = new MyClass();
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
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.


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:
subject: Throwing Exceptions
It's not a secret anymore!