0

最初に、私は以下を読んだと言わなければなりません: http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html !

コンパイルしようとすると発生するエラーは次のとおりです。

TestException.java:14: error: incompatible types

static void doRisky(String test) throws ScaryException
                                                ^
required: Throwable
found:    ScaryException

TestException.java:19: error: incompatible types
                        throw new ScaryException();
                                      ^
required: Throwable
found:    ScaryException

TestException.java:34: error: incompatible types
                catch ( ScaryException  se)
                        ^
  required: Throwable
  found:    ScaryException

3 errors

エラーメッセージが何らかのヒントを与えていると思いますが、それが何であるかわかりません。

import java.lang.*;
/** add this as to get it to compile */
class ScaryException extends Exception
{
/** how come I can't put code here? */
}

public class  TestException {

    static void doRisky(String test) throws ScaryException{
        System.out.println ("start risky");    
        if ("yes".equals(test)) {
            throw new ScaryException();
        }
        System.out.println("end risky");
    }

    public static void main(String [] args){
        String test = "no";
        try{
            System.out.println("start try");
            doRisky(test);
            doRisky("yes");
            System.out.println("end try");
        }
        catch ( ScaryException  se){
            System.out.println("Got What " + se);
        }
        finally{
            System. out. println( "finally") ;
        }

        System.out.println("end of main");
    }
}
4

2 に答える 2

2

Java 例外は拡張する必要がありますThrowable(直接、またはExceptionそのサブクラスの 1 つを拡張して間接的に行うことが望ましい)。どうやらあなたのScaryExceptionクラスはそうではありません。

于 2013-03-05T20:47:10.180 に答える
1

これをコンパイルするにScaryExceptionは、拡張するクラスThrowableまたはそのサブクラスの 1 つでなければなりません。ScaryExceptionがまったく存在しないか、 の子を拡張していませんThrowable

于 2013-03-05T20:47:24.157 に答える