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 why when try won't throw a exception ,it can not compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why when try won Watch "why when try won New topic

why when try won't throw a exception ,it can not compile?

Jianfeng Qian
Ranch Hand

Joined: Jan 25, 2005
Posts: 83
is it because try is in the main?or because it is in the static method?
or something else?
I'm comfused.

public class MyThread extends Thread
String myName;

MyThread(String name)
myName = name;

public void run()
for(int i=0; i<100;i++)

public static void main(String args[])
MyThread mt1 = new MyThread("mt1");
MyThread mt2 = new MyThread("mt2");
// XXX
//throw new InterruptedException();
if add the upper line ,compile failed and say that can't reach the next line.
//throw new InterruptedException();
//when i add upper line,it can compile
catch(InterruptedException ex)

Jianfeng Qian<br />SCJP 1.4, SCBCD 5.0(beta)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
1) use code tags
2) what compiler error are you getting?
3) is this really a question related to the SCJP exam?

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
If you throw an exception

throw new InterruptedException();

the line below it will never be executed, that's why the compiler gives that message.
Once the exception is thrown, the program is going to the next catch (or finally) block. If there isn't one until it reaches main, your program will be halted.

Leandro Melo
SCJP 1.4, SCWCD 1.4
I agree. Here's the link:
subject: why when try won't throw a exception ,it can not compile?
It's not a secret anymore!