Java:自定义异常错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2631345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:07:09  来源:igfitidea点击:

Java: custom-exception-error

javaerror-handlingcustom-errors

提问by hhh

$ javac TestExceptions.java 
TestExceptions.java:11: cannot find symbol
symbol  : class test
location: class TestExceptions
            throw new TestExceptions.test("If you see me, exceptions work!");
                                    ^
1 error

Code

代码

import java.util.*;
import java.io.*;

public class TestExceptions {
    static void test(String message) throws java.lang.Error{
        System.out.println(message);
    }   

    public static void main(String[] args){
        try {
             // Why does it not access TestExceptions.test-method in the class?
            throw new TestExceptions.test("If you see me, exceptions work!");
        }catch(java.lang.Error a){
            System.out.println("Working Status: " + a.getMessage() );
        }
    }
}

回答by danben

TestExceptions.testreturns type void, so you cannot throwit. For this to work, it needs to return an object of a type that extends Throwable.

TestExceptions.test返回 type void,所以你不能throw。为此,它需要返回一个扩展类型的对象Throwable

One example might be:

一个例子可能是:

   static Exception test(String message) {
        return new Exception(message);
    } 

However, this isn't very clean. A better pattern would be to define a TestExceptionclass that extends Exceptionor RuntimeExceptionor Throwable, and then just throwthat.

然而,这不是很干净。更好的方式是定义一个TestException扩展类ExceptionRuntimeExceptionThrowable,然后只是throw说。

class TestException extends Exception {
   public TestException(String message) {
     super(message);
   }
}

// somewhere else
public static void main(String[] args) throws TestException{
    try {
        throw new TestException("If you see me, exceptions work!");
    }catch(Exception a){
        System.out.println("Working Status: " + a.getMessage() );
    }
}

(Also note that all classes in package java.langcan be referenced by their class name rather than their fully-qualified name. That is, you don't need to write java.lang.)

(还要注意包中的所有类java.lang都可以通过它们的类名而不是它们的完全限定名来引用。也就是说,你不需要写java.lang。)

回答by Dave Jarvis

Working Code

工作代码

Try this:

试试这个:

public class TestExceptions extends Exception {
    public TestExceptions( String s ) {
      super(s);
    }

    public static void main(String[] args) throws TestExceptions{
        try {
            throw new TestExceptions("If you see me, exceptions work!");
        }
        catch( Exception a ) {
            System.out.println("Working Status: " + a.getMessage() );
        }
    }
}

Problems

问题

There are a number of issues with the code you posted, including:

您发布的代码存在许多问题,包括:

  • Catching Errorinstead of Exception
  • Using a static method to construct the exception
  • Not extending Exceptionfor your exception
  • Not calling the superclass constructor of Exceptionwith the message
  • 捕捉Error而不是Exception
  • 使用静态方法构造异常
  • 不扩展Exception您的例外
  • 不调用Exception带有消息的超类构造函数

The posted code resolves those issues and displays what you were expecting.

发布的代码解决了这些问题并显示了您的期望。