本文翻译自:Uncatchable ChuckNorrisException

Is it possible to construct a snippet of code in Java that would make a hypothetical java.lang.ChuckNorrisException uncatchable? 是否有可能在Java中构造一段代码,使得假设的java.lang.ChuckNorrisException无法捕获?

Thoughts that came to mind are using for example interceptors or aspect-oriented programming . 想到的想法是使用例如拦截器或面向方面的编程 。


#1楼

参考:https://stackoom.com/question/wFe2/无法捕获的ChuckNorrisException


#2楼

Any code can catch Throwable. 任何代码都可以捕获Throwable。 So no, whatever exception you create is going to be a subclass of Throwable and will be subject to being caught. 所以不,你创建的任何异常都将成为Throwable的子类,并且会被捕获。


#3楼

No. All exceptions in Java must subclass java.lang.Throwable , and although it may not be good practice, you can catch every type of exception like so: 不是.Java中的所有异常都必须是java.lang.Throwable子类,虽然它可能不是一个好习惯,但你可以像这样捕获每种类型的异常:

try {//Stuff
} catch ( Throwable T ){//Doesn't matter what it was, I caught it.
}

See the java.lang.Throwable documentation for more information. 有关更多信息,请参阅java.lang.Throwable文档。

If you're trying to avoid checked exceptions (ones that must be explicitly handled) then you will want to subclass Error, or RuntimeException. 如果您试图避免检查异常 (必须显式处理的异常 ),那么您将需要子类Error或RuntimeException。


#4楼

With such an exception it would obviously be mandatory to use a System.exit(Integer.MIN_VALUE); 有了这样的例外,显然必须使用System.exit(Integer.MIN_VALUE); from the constructor because this is what would happen if you threw such an exception ;) 来自构造函数,因为如果你抛出这样的异常会发生这种情况;)


#5楼

Any exception you throw has to extend Throwable, so it can be always caught. 您抛出的任何异常都必须扩展Throwable,因此可以始终捕获它。 So answer is no. 所以答案是否定的。

If you want to make it difficult to handle, you can override methods getCause(), getMessage() , getStackTrace() , toString() to throw another java.lang.ChuckNorrisException . 如果要使其难以处理,可以覆盖方法getCause(), getMessage()getStackTrace()toString()以抛出另一个java.lang.ChuckNorrisException


#6楼

I haven't tried this, so I don't know if the JVM would restrict something like this, but maybe you could compile code which throws ChuckNorrisException , but at runtime provide a class definition of ChuckNorrisException which does not extend Throwable . 我没有试过这个,所以我不知道JVM是否会限制这样的东西,但也许你可以编译抛出ChuckNorrisException代码,但是在运行时提供了ChuckNorrisException的类定义,它不会扩展Throwable

UPDATE: 更新:

It doesn't work. 它不起作用。 It generates a verifier error: 它会生成验证错误:

Exception in thread "main" java.lang.VerifyError: (class: TestThrow, method: ma\
in signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestThrow.  Program will exit.

UPDATE 2: 更新2:

Actually, you can get this to work if you disable the byte code verifier! 实际上,如果禁用字节码验证器,您可以使用它! ( -Xverify:none ) -Xverify:none

UPDATE 3: 更新3:

For those following from home, here is the full script: 对于那些在家的人,这里是完整的脚本:

Create the following classes: 创建以下类:

public class ChuckNorrisExceptionextends RuntimeException // <- Comment out this line on second compilation
{public ChuckNorrisException() { }
}public class TestVillain {public static void main(String[] args) {try {throw new ChuckNorrisException();}catch(Throwable t) {System.out.println("Gotcha!");}finally {System.out.println("The end.");}}
}

Compile classes: 编译类:

javac -cp . TestVillain.java ChuckNorrisException.java

Run: 跑:

java -cp . TestVillain
Gotcha!
The end.

Comment out "extends RuntimeException" and recompile ChuckNorrisException.java only : 注释掉“extends RuntimeException”并仅重新编译ChuckNorrisException.java

javac -cp . ChuckNorrisException.java

Run: 跑:

java -cp . TestVillain
Exception in thread "main" java.lang.VerifyError: (class: TestVillain, method: main signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestVillain.  Program will exit.

Run without verification: 运行时无需验证:

java -Xverify:none -cp . TestVillain
The end.
Exception in thread "main"

无法捕获的ChuckNorrisException相关推荐

  1. StackOverflow上面 7个最好的Java答案

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 StackOverflow发展到目前,已经成为了全球开发者的金矿. ...

  2. StackOverflow 上面最流行的 7 个 Java 问题!

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 原文:https://dwz.cn/Boy5tcHJ 译 ...

  3. StackOverflow 上面最流行的 7 个 Java 问题!| 值得一看

    点击上方"朱小厮的博客",选择"设为星标" 后台回复"加群",加入新技术 StackOverflow发展到目前,已经成为了全球开发者的金矿. ...

  4. Go 知识点(14) — Go 多协程(单个协程触发panic会导致其它所有协程挂掉,每个协程只能捕获到自己的 panic 不能捕获其它协程)

    在多协程并发环境下,我们常常会碰到以下两个问题.假设我们现在有 2 个协程,我们叫它们协程 A 和 B . [问题1]如果协程 A 发生了 panic ,协程 B 是否会因为协程 A 的 panic ...

  5. Python+OpenCV 图像处理系列(2)—— 视频捕获、播放和保存

    1.视频捕获 为了获取视频,首先需要创建一个 VideoCapture 类对象.它的参数可以是设备的索引号,或者是一个视频文件.设备索引号就是在指定要使用的摄像头.一般的笔记本电脑都有内置摄像头.所以 ...

  6. [JS] 事件冒泡,阻止事件冒泡,事件的三个阶段(捕获,目标,冒泡)

    事件冒泡 添加三个套在一起的div元素,在最里面放一个button,感受事件触发时从里到外"冒泡"的过程. 给每个div都加一个事件:点击时就alert test.html < ...

  7. 针对JavaScript的常用事件、对象捕获和使用技巧

    事件源对象  event.srcElement.tagName  event.srcElement.type  捕获释放  event.srcElement.setCapture();   event ...

  8. 添加引用方式抛出和捕获干净的WebService异常

    转载:http://www.cnblogs.com/ahdung/p/3953431.html 说明:[干净]指的是客户端在捕获WebService(下称WS)抛出的异常时,得到的ex.Message ...

  9. Qt/Linux 下的摄像头捕获(Video4Linux2)

    Linux下使用各种设备是一件令人兴奋的事情.在Unix的世界里,用户与硬件打交待总是简单的.最近笔者在Linux下搞了摄像头的开发,有一点感想发于此处. Linux中操作一个设备一般都是打开(ope ...

最新文章

  1. DEDE-Function ereg_replace() is deprecated in ..line 2
  2. java 常见几种发送http请求案例
  3. React 入门之路
  4. Linux-部署应用到生产环境Tomcat项目-全流程(图文教程)
  5. python定义变量名的时候、需要注意问题_python中将函数赋值给变量时需要注意的一些问题...
  6. Android之通过HttpURLConnection.getResponseCode状态码抛出异常的问题以及解决方法
  7. 摊上事了?中国卖家对亚马逊发起集体诉讼:3000多账号被封 损失惨重
  8. KeyMob--移动广告聚合平台界的黑马
  9. 来到深圳奋斗的这些年(不断更新!)
  10. python抓取网页内容并下载图片
  11. samsung-smart app 开发
  12. Nginx系列(4):Web服务器分析(理论上)
  13. android fastboot 工具,fastboot工具中文帮助文档
  14. 成都信息工程大学上岸软件工程专硕经验分享
  15. restsharp text html,c# – 使用RestSharp发送HTTP POST Multipart / form-data字段
  16. html制作钟表盘,CSS3简易表盘时钟
  17. html title中加图标,科技常识:HTML中title前面小图标的实现_如何给网页标题添加icon小图标...
  18. Error: [$injector:unpr]错误解释
  19. (一)BST树(二叉搜索树)
  20. 2013各大公司的待遇如下

热门文章

  1. IO操作Dex文件加密,APK加固项目实战
  2. Android基础建设之Activity,ViewRoot,WindowManager,Window,View
  3. 问一下大家个3D问题:旋转和缩放以及平移中如何插值矩阵?
  4. Android蓝牙开发其二
  5. 编译系统总结篇-Android10.0编译系统(十一)
  6. EasyPR中文开源车牌识别系统 开发详解
  7. (0047)iOS开发之nil/Nil/NULL的区别
  8. linux脚本 程序输入,[转]Linux中shell脚本如何自动输入…
  9. 20175330 数据结构-单链表(选做)
  10. UILable在Autolayout模式下面自动调节字体大小