Java 如何编写自定义异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1070590/
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
How can I write custom Exceptions?
提问by Johanna
How can I create a new Exception
different from the pre-made types?
如何创建Exception
与预制类型不同的新类型?
public class InvalidBankFeeAmountException extends Exception{
public InvalidBankFeeAmountException(String message){
super(message);
}
}
It will show the warning for the InvalidBankFeeAmountExceptionwhich is written in the first line.
它将显示第一行中写入的InvalidBankFeeAmountException的警告。
采纳答案by jjnguy
All you need to do is create a new class
and have it extend Exception
.
您需要做的就是创建一个新的class
并拥有它extend Exception
。
If you want an Exception
that is unchecked, you need to extend RuntimeException
.
如果你想要一个Exception
未选中的,你需要extend RuntimeException
.
Note: A checked Exception
is one that requires you to either surround the Exception
in a try
/catch
block or have a 'throws
' clause on the method declaration. (like IOException
) Unchecked Exceptions
may be thrown just like checked Exceptions
, but you aren't required to explicitly handle them in any way (IndexOutOfBoundsException
).
注意:这个方法会Exception
是一个需要你要么环绕Exception
在try
/catch
块或有一个“throws
在方法声明”的条款。(例如IOException
) UncheckedExceptions
可能会像 checked 一样被抛出Exceptions
,但您不需要以任何方式(IndexOutOfBoundsException
)显式处理它们。
For example:
例如:
public class MyNewException extends RuntimeException {
public MyNewException(){
super();
}
public MyNewException(String message){
super(message);
}
}
回答by toolkit
just extend either
只是延长
Exception
, if you want your exception to be checked (i.e: required in a throws clause)RuntimeException
, if you want your exception to be unchecked.
Exception
, 如果您希望检查您的异常(即:需要在 throws 子句中)RuntimeException
, 如果您希望您的异常不被检查。
回答by Jon
Take a look at:
看一眼:
http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html?page=1
http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html?page=1
An example is given there on page 2:
第 2 页上给出了一个示例:
public class DuplicateUsernameException
extends Exception {
public DuplicateUsernameException
(String username){....}
public String requestedUsername(){...}
public String[] availableNames(){...}
}
along with a set of guidelines for when and why you'd create your own exceptions.
以及有关何时以及为何创建自己的例外的一组准则。
回答by Jim Ferrans
Be sure not to go overboard with exceptions, especially checked exceptions. I'd recommend reading Chapter 9 of Joshua Bloch's Effective Java, and in particular his Item 60 (Favor the use of standard exceptions). His recommendations also include using checked exceptions for exceptions that can be recovered from, using unchecked exceptions (RuntimeExceptions) for programming errors, and avoiding the unnecessary use of checked exceptions.
确保不要过度使用异常,尤其是检查异常。我建议阅读 Joshua Bloch 的 Effective Java 的第 9 章,尤其是他的 Item 60(Favor the use of standard exceptions)。他的建议还包括对可以从中恢复的异常使用受检异常,对编程错误使用非受检异常(RuntimeExceptions),以及避免不必要地使用受检异常。
If an InvalidBankAccount exception is thrown whenever an programming error is found, you probably just want to throw a standard unchecked Java IllegalStateException instead. (This neatly sidesteps the need to declare serialVersionUID.)
如果在发现编程错误时抛出 InvalidBankAccount 异常,您可能只想抛出标准的未经检查的 Java IllegalStateException。(这巧妙地回避了声明 serialVersionUID 的需要。)