为什么在Java中捕获异常,当您可以捕获Throwables?(Why catch Exceptions in Java, when you can catch Throwables?)
我们最近遇到一个Java服务器应用程序的问题,应用程序抛出错误,因为Error是Throwable的一个单独的子类,我们只捕获了异常。
我们通过捕捉Throwables而不是异常来解决现在的问题,但是这让我想到为什么你会想要捕获异常,而不是Throwables,因为你会错过错误。
那么, 你为什么要捕捉异常,当你可以捕捉到Throwables ?
We recently had a problem with a Java server application where the application was throwing Errors which were not caught because Error is a separate subclass of Throwable and we were only catching Exceptions.
We solved the immediate problem by catching Throwables rather than Exceptions, but this got me thinking as to why you would ever want to catch Exceptions, rather than Throwables, because you would then miss the Errors.
So, why would you want to catch Exceptions, when you can catch Throwables?
原文:https://stackoverflow.com/questions/581878
最满意答案
它不是基于可转换为参数类型的类型数量 - 由于隐式转换,是否对一个重载有效的任何值对另一个重载有效。
例如,有一个从
String
到Object
的隐式转换,但反之亦然,所以String
比Object
更具体。同样地,从
B
到A
有一个隐式的转换,但反之亦然,所以B
比A
更具体。然而,使用
A
和E
,两者都不比其他的更具体 - 没有从A
转换为E
,也不从E
转换为A
这就是为什么重载解析失败。JLS的相关位实际上是15.12.2.5 ,其中包括这可能会使您更容易理解:
非正式的直觉是,如果第一种方法处理的任何调用可以传递给另一种方法而没有编译时错误,则一种方法比另一种方法更具体。
所以如果你有:
void foo(String x) void foo(Object x)
foo(String)
处理的每个调用都可以由foo(Object)
处理,但情况并非如此。 (例如,您可以调用foo(new Object())
,并且无法由foo(String)
处理。)It's not based on the number of types that are convertible to the parameter type - it's whether any value that's valid for one overload is valid for another, due to implicit conversions.
For example, there's an implicit conversion from
String
toObject
, but the reverse isn't true, soString
is more specific thanObject
.Likewise there's an implicit conversion from
B
toA
, but the reverse isn't true, soB
is more specific thanA
.With
A
andE
however, neither is more specific than the other - there no conversion fromA
toE
, and no conversion fromE
toA
. That's why overload resolution fails.The relevant bit of the JLS is actually 15.12.2.5, which includes this that might make it easier for you to understand:
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.
So if you have:
void foo(String x) void foo(Object x)
every invocation handled by
foo(String)
could be handled byfoo(Object)
, but the reverse is not the case. (For example, you could callfoo(new Object())
and that couldn't be handled byfoo(String)
.)
相关问答
更多-
用scala中的重载构造函数定义你自己的异常(define your own exceptions with overloaded constructors in scala)[2023-12-26]
cause默认值为空。 对于message它可以是cause.toString()或null: val e1 = new RuntimeException() e.getCause // res1: java.lang.Throwable = null e.getMessage //res2: java.lang.String = null val cause = new RuntimeException("cause msg") val e2 = new RuntimeException(cause ... -
我假设你来自Java-ish背景,所以有几个关键的区别需要指出。 class Computer(object): """Docstrings are used kind of like Javadoc to document classes and members. They are the first thing inside a class or method. You probably want to extend object, to make it a "new-sty ...
-
它不是基于可转换为参数类型的类型数量 - 由于隐式转换,是否对一个重载有效的任何值对另一个重载有效。 例如,有一个从String到Object的隐式转换,但反之亦然,所以String比Object更具体。 同样地,从B到A有一个隐式的转换,但反之亦然,所以B比A更具体。 然而,使用A和E ,两者都不比其他的更具体 - 没有从A转换为E ,也不从E转换为A 这就是为什么重载解析失败。 JLS的相关位实际上是15.12.2.5 ,其中包括这可能会使您更容易理解: 非正式的直觉是,如果第一种方法处理的任何调用可以 ...
-
重载的构造函数(Overloaded constructors)[2023-06-13]
new Employee(); 正在调用 public Employee(){ this("JJ", 0); System.out.println(name +" "+ idNumber); } 在这个构造函数中 this("JJ", 0); 正在调用 public Employee(String name, int num) 构造函数,以调用结束 System.out.println(name +" 2nd "+ idNumber); 。 负责印刷 JJ 2nd 0 当this("JJ" ... -
就个人而言,我会使用classmethods。 WrapperClass.fromSpambar(spambar) 这不是puzzlin imho 。 如果你不能在C ++中重载函数,你也需要回退到这种方法。 如果您可以在初始化时接受一些重加权操作,则可以通过定义“模式”或类似方法来实现一种方法来识别构造函数的调用方式。 即args和kwargs的正则表达式。 ;) 我没有看到从Python对象获取thisptr的问题。 cdef WrapperClass wrpclsi if isinstance(in ...
-
对不起! 但我没有得到这个! “ 然而,当使用构造函数时会出现问题 ” 能否请您提供更多详情! 谢谢! 谢谢! 为了您的详细信息 只需在第一行的public static void main中调用input方法 import java.util.*; public class Time{ public static void main(String[] args) { input(); TimeCalculations time1 = new TimeCalculations(); ...
-
“重载”值构造函数(“Overloaded” value constructors)[2023-08-15]
你可以使用类型类来使一个函数的行为既可以是Point -> Point -> Rectangle和Float -> Float -> Float -> Float -> Rectangle ,但我不会主张它。 这对增益会很麻烦。 无论如何,我认为你无论如何都可以在模式匹配中使用如此重载的名称。 我看到它的方式,如果你只是通过解构它们并使用原始Float值来使用Point值,那么你并没有真正从中获得那么多,所以你可以通过获得它来解决你的问题彻底摆脱它。 但是你错过了一个黄金机会来实现一个直接调整点的函数! 对 ... -
是的,我会重构这个。 智能感知只是非常有用,并且盯着一个5个参数的构造函数,该构造函数需要3个看似随机的字符串,试图找出哪一个意味着什么不会提高代码可读性。 在VB(或C#4.0)中,我仍然使用构造函数,但使用命名参数。 对于C#3.0,我会创建一个单独的类来保存初始化信息: class ObjectSettings { public string StrA { get; set; } public string StrB { get; set; } ... public s ...
-
您可以使用模板轻松完成: class A; class B; template
class C { public: C(AorB aorb) : aorb_(aorb) { } private: AorB aorb_; }; 这样做的是,在类C ,标识符AorB可以用作任何其他类,实际上它甚至不必是A或B的实例,但可以是任何类。 可以像这样使用: A myA; B myB; C myCWithA(myA); C my ... -
执行重载 - 但是在默认构造函数中提供默认参数(或者根据具体情况提供最低级别): public class ClassSecond { public string Name { get; set; } public int Height { get; set; } public int Weight { get; set; } public ClassSecond(string name) { Name = name; Height ...