Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编译错误:在相应的try语句体中从未引发异常_Java_Exception_Checked Exceptions - Fatal编程技术网

Java 编译错误:在相应的try语句体中从未引发异常

Java 编译错误:在相应的try语句体中从未引发异常,java,exception,checked-exceptions,Java,Exception,Checked Exceptions,为了更好地学习Java,我一直在尝试理解异常处理。我无法理解为什么下面的代码无法编译 编译器消息是: TestExceptionHandling.java:12: error: exception StupidException is never thrown in body of corresponding try statement catch (StupidException stupidEx) { ^ 在try块中,调用方

为了更好地学习Java,我一直在尝试理解异常处理。我无法理解为什么下面的代码无法编译

编译器消息是:

TestExceptionHandling.java:12: error: exception StupidException is never thrown in body of corresponding try statement
                catch (StupidException stupidEx) {
                ^
try
块中,调用方法exTest.doExTest()。在这个方法中,我捕获一个InterruptedException,并在它的
catch
块中抛出一个新的StupidException

那么为什么编译器说它没有被抛出呢?我错过了什么?请任何专家帮我看看我的错误好吗

  public class TestExceptionHandling {

    public static void main(String argv[]) {

        String output = "";

        try {
            output = "\nCalling method doExTest:\n";
            exTest.doExTest();
        }

        catch (StupidException stupidEx) {
            System.out.println("\nJust caught a StupidException:\n   " + stupidEx.toString());
        }

        System.out.println(output);
    }   
}   

class exTest {

    static long mainThreadId;

    protected static void doExTest() {  //throws StupidException 

        mainThreadId = Thread.currentThread().getId();
        Thread t = new Thread(){
            public void run(){
                System.out.println("Now in run method, going to waste time counting etc. then interrupt main thread.");
                // Keep the cpu busy for a while, so other thread gets going...
                for (int i = 0; i < Integer.MAX_VALUE; i++) {
                    int iBoxed = (int)new Integer(String.valueOf(i));
                    String s = new String("This is a string" + String.valueOf(iBoxed));
                }   
                // find thread to interrupt...
                Thread[] threads = new Thread[0];
                Thread.enumerate(threads);
                for (Thread h: threads) { 
                    if (h.getId() == mainThreadId) {
                        h.interrupt();
                    }
                }
            }
        };

        t.start();

        try {
            Thread.sleep(5000);
        }

        catch (InterruptedException e){
            System.out.println("\nAn InterruptedException " + e.toString() + " has occurred.   Exiting...");
            throw new StupidException("Got an InterruptedException ", e); // ("Got an InterruptedException.   Mutated to StupidException and throwing from doExTest to caller...", e);
        }   

    }
}

class StupidException extends Exception {

    public StupidException(String message, Throwable t) {
        super(message + " " + t.toString());
    }

    public String toString() {
        return "Stupid Exception:  " + super.toString();
    }
}
公共类TestExceptionHandling{
公共静态void main(字符串argv[]){
字符串输出=”;
试一试{
output=“\n调用方法doExTest:\n”;
doExTest();
}
捕获(StupidException stupidEx){
System.out.println(“\n刚刚捕获了一个StupidException:\n”+stupidEx.toString());
}
系统输出打印项次(输出);
}   
}   
类外部测试{
静态长主线程ID;
受保护的静态void doExTest(){//抛出StupidException
mainThreadId=Thread.currentThread().getId();
线程t=新线程(){
公开募捐{
System.out.println(“现在运行方法,将浪费时间计数等,然后中断主线程”);
//让cpu保持忙碌一段时间,以便其他线程继续运行。。。
对于(int i=0;i
方法需要显式声明它们抛出异常(运行时异常除外,这有点不同)。尝试将您的
doExTest
方法声明为

protected static void doExTest() throws StupidException {
    ...
}

doExTest()
必须声明为抛出
StupidException
。好的,这是可行的:我想可以在调用方法中声明它,在那里我捕获它。越来越清楚了!