首页 \ 问答 \ 为什么在Java中捕获异常,当您可以捕获Throwables?(Why catch Exceptions in Java, when you can catch Throwables?)

为什么在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
更新时间:2023-09-22 17:09

最满意答案

它不是基于可转换为参数类型的类型数量 - 由于隐式转换,是否对一个重载有效的任何值对另一个重载有效。

例如,有一个从StringObject的隐式转换,但反之亦然,所以StringObject更具体。

同样地,从BA有一个隐式的转换,但反之亦然,所以BA更具体。

然而,使用AE ,两者都不比其他的更具体 - 没有从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 to Object, but the reverse isn't true, so String is more specific than Object.

Likewise there's an implicit conversion from B to A, but the reverse isn't true, so B is more specific than A.

With A and E however, neither is more specific than the other - there no conversion from A to E, and no conversion from E to A. 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 by foo(Object), but the reverse is not the case. (For example, you could call foo(new Object()) and that couldn't be handled by foo(String).)

相关问答

更多
  • 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 ,其中包括这可能会使您更容易理解: 非正式的直觉是,如果第一种方法处理的任何调用可以 ...
  • 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(); ...
  • 你可以使用类型类来使一个函数的行为既可以是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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。