| Author |
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++) { System.out.println(myName); } } public static void main(String args[]) { try { MyThread mt1 = new MyThread("mt1"); MyThread mt2 = new MyThread("mt2"); mt1.start(); // XXX //throw new InterruptedException(); if add the upper line ,compile failed and say that can't reach the next line. mt2.start(); //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?
|
42
|
 |
Leandro Melo
Ranch Hand
Joined: Mar 27, 2004
Posts: 401
|
|
Hi. 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 <br />SCJP 1.4, SCWCD 1.4<br /><a href="http://www.pazbrasil.org/" target="_blank" rel="nofollow">http://www.pazbrasil.org/</a>
|
 |
 |
|
|
subject: why when try won't throw a exception ,it can not compile?
|
|
|