java JAVA中未处理的异常类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16885318/
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-11-01 00:16:11  来源:igfitidea点击:

Unhandled Exception Type in JAVA

javaexceptionexception-handlingunhandled-exception

提问by Will

I have a two classes in the same package in JAVA.

我在 JAVA 的同一个包中有两个类。

One class in which I have a constructor an exception I tried to create myself:

我有一个构造函数的一个类,我试图自己创建一个异常:

public class ZooException extends Exception {

        public ZooException(String error) {
            super(error);
        }   
}

Another class needs to call this exception at one point:

另一个类需要在某一时刻调用这个异常:

public class Zoo<T extends Animal>

Zoo(int capacity) {
        if (capacity <= 1) {
        throw new ZooException("Zoo capacity must be larger than zero");
    }
}

I notice two things here

我注意到两件事

  1. In the ZooException class, I get a warning: "The serializable class CageException does not declare a static final serialVersionUID field of type long"
  2. In the Zoo class, on the line that starts with "throw new", I get a compile error: "Unhandled exception type CageException"
  1. 在 ZooException 类中,我收到一条警告:“可序列化类 CageException 未声明 long 类型的静态最终 serialVersionUID 字段”
  2. 在 Zoo 类中,在以“throw new”开头的那一行,我得到一个编译错误:“Unhandled exception type CageException”

Any ideas on what I can do to solve this error in the Zoo class? Thank you in advance!

关于我可以做些什么来解决 Zoo 类中的这个错误的任何想法?先感谢您!

回答by Will

You are extending Exception, a checked exception, this means that any method which throws that exception needs to say so:

您正在扩展Exception一个已检查的异常,这意味着抛出该异常的任何方法都需要这样说:

Zoo(int capacity) throws ZooException {

And any code calling that constructor will have to try {} catch {}or throw it again.

任何调用该构造函数的代码都将不得不try {} catch {}或再次抛出它。

If you don't want it to be checked, use extends RuntimeExceptioninstead

如果您不想检查它,请extends RuntimeException改用

回答by Mordechai

About unchecked exceptions @Will Pis right.

关于未经检查的异常@Will P是对的。

About serialVersionUID, this means - since Exception is Serializable - if you anytime decide that a previous version of your class should be incompatible with your newer version ( usually for public APIs), for example, that class had undergone major changes, simply change the unique id, and reading an object of the old version would throw an exception.

关于serialVersionUID,这意味着——因为异常是可序列化的——如果你任何时候决定你的类的先前版本应该与你的新版本不兼容(通常用于公共API),例如,该类发生了重大变化,只需更改唯一的id,读取旧版本的对象会抛出异常。

回答by Aurand

Zoo(int capacity) throws ZooException {
    if (capacity <= 1) {
    throw new ZooException("Zoo capacity must be larger than zero");
}

You must declare checked exceptions or handle them with a try-catch bock. Try reading up on exceptions: http://docs.oracle.com/javase/tutorial/essential/exceptions/

您必须声明已检查的异常或使用 try-catch 块处理它们。尝试阅读异常:http: //docs.oracle.com/javase/tutorial/essential/exceptions/