jQuery in Action, 2nd edition*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why when try won Watch "why when try won New topic
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
SCJP 1.4, SCWCD 1.4
 
 
subject: why when try won't throw a exception ,it can not compile?
 
Similar Threads
Another Question (.... Threads / join() )
Thread doubt ??
notifyAll()
help regarding synchronized methods
exception is never thrown in try compiler error