我有Java主类,在类中,我启动一个新线程,在主,它等待直到线程死亡.在某些时刻,我从线程中抛出一个运行时异常,但是我无法捕获从主类中的线程抛出的异常.
这是代码:
public class Test extends Thread
{
public static void main(String[] args) throws InterruptedException
{
Test t = new Test();
try
{
t.start();
t.join();
}
catch(RuntimeException e)
{
System.out.println("** RuntimeException from main");
}
System.out.println("Main stoped");
}
@Override
public void run()
{
try
{
while(true)
{
System.out.println("** Started");
sleep(2000);
throw new RuntimeException("exception from thread");
}
}
catch (RuntimeException e)
{
System.out.println("** RuntimeException from thread");
throw e;
}
catch (InterruptedException e)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?
在我的Web应用程序中创建了一个用户定义的异常,它扩展了Exception.Is it Checked或unchecked exception
public class InvalidDataException extends Exception{
public InvalidDataException() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param arg0
*/
public InvalidDataException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
Run Code Online (Sandbox Code Playgroud)