Java 中的一般异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792149/
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
General Exception in Java
提问by Kyle Rozendo
I am looking to throw a general Exceptionin Java.
我想Exception在 Java 中抛出一个将军。
The "situation" is basically if an empty line is encountered, an exception is thrown and the blank line ignored.
“情况”基本上是如果遇到空行,则抛出异常并忽略空行。
Now, I come from a C# background so I would just throw a normal Exception. In Java, is there one? I can't seem to find it.
现在,我来自 C# 背景,所以我只会抛出一个普通的Exception. 在Java中,有吗?我好像找不到
I know I could assert, but would this solve the problem correctly using an AssertionException?
我知道我可以断言,但这是否可以使用AssertionException?
Thanks,
谢谢,
Kyle
凯尔
EDIT:
编辑:
Thanks for the answers. Unfortunately it was my own foolishness that allowed me to miss Exception, which I found just before checking back. I accepted Bills answer for the assertion comment, considering I couldn't delete the question due to too many up votes (in like 10 seconds I might add, hehe).
感谢您的回答。不幸的是,是我自己的愚蠢让我错过了Exception,我在回来之前发现了这一点。我接受了 Bills 对断言评论的回答,考虑到由于太多的投票我无法删除该问题(大约 10 秒内我可能会添加,呵呵)。
回答by Bill the Lizard
Java has a normal Exceptionclass, but you almost never want to throw it. I prefer to extend it (or another exception type) to make a more specific exception to throw for your specific situation.
Java 有一个普通的Exception类,但您几乎从不想抛出它。我更喜欢扩展它(或其他异常类型)以针对您的特定情况抛出更具体的异常。
Also, you shouldn't use assertions in place of throwing exceptions in your code. Assertions may be disabled (and are disabled by default) so you can't count on them being there when your code is run by the end user. There's a lot of good information in Sun's article Programming With Assertions.
此外,您不应使用断言代替在代码中抛出异常。断言可能被禁用(默认情况下是禁用的),因此当您的代码由最终用户运行时,您不能指望它们在那里。Sun 的文章Programming With Assertions 中有很多很好的信息。
回答by Greg Hewgill
I'm wondering why you would need to throw an exception in the first place. If your goal is to ignore blank lines, then an ifstatement sounds like a better solution. Don't use exceptions as a substitute for normal flow control.
我想知道为什么您首先需要抛出异常。如果您的目标是忽略空行,那么if语句听起来是更好的解决方案。不要使用异常来代替正常的流控制。
回答by Dathan
Bozho is right. But the more complete answer is that in C# all exceptions must inherit from System.Exception, whereas in Java you can throw anything that inherits from java.lang.Throwable. java.lang.Exception inherits java.lang.Throwable, and is the superclass of most exceptions used in Java. You may alternatively want to throw a java.lang.Error instead, though, or create your own throwable class to use.
博佐说得对。但更完整的答案是,在 C# 中,所有异常都必须从 System.Exception 继承,而在 Java 中,您可以抛出从 java.lang.Throwable 继承的任何内容。java.lang.Exception 继承了 java.lang.Throwable,是 Java 中使用的大多数异常的超类。不过,您也可以选择抛出 java.lang.Error ,或者创建自己的可抛出类来使用。
Otherwise, the semantics are very similar:
否则,语义非常相似:
throw new Exception();
However, most of the time Exceptions should be thrown only for special cases. If you expect to receive blank lines, and expect to ignore them, your code should handle this case without throwing an exception.
但是,大多数情况下只应在特殊情况下抛出异常。如果您希望收到空行,并希望忽略它们,您的代码应该处理这种情况而不抛出异常。

