有人请解释之间的差异java.lang.RuntimeException和java.lang.Exception?如果我创建自己的异常,如何决定扩展哪一个?
我在SO上进行了这个伟大的讨论,标题为:针对已检查异常的情况,但是我无法遵循应该使用RuntimeException的地方以及它与正常异常及其子类的不同之处.谷歌搜索给了我一个复杂的答案,也就是说,它应该用于处理编程逻辑错误,并且应该在没有正常情况发生时抛出,例如在switch-case结构的默认块中.
你能否在这里详细解释一下RuntimeException.谢谢.
我有两段代码:
class PreciseRethrow {
public static void main(String[] str) {
try {
foo();
} catch (NumberFormatException ife) {
System.out.println(ife);
}
}
static private void foo() throws NumberFormatException {
try {
int i = Integer.parseInt("ten");
} catch (Exception e) {
throw e;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并且:
class PreciseRethrow {
public static void main(String[] str) {
try {
foo();
} catch (NumberFormatException ife) {
System.out.println(ife);
}
}
static private void foo() throws NumberFormatException {
try {
int i = Integer.parseInt("ten");
} catch …Run Code Online (Sandbox Code Playgroud)