背景故事

影片《金刚》是2005年上映的一部冒险电影,它讲述1933年的美国,一名勇于冒险的企业家及电影制作者,率领摄制队伍到荒岛拍摄,其中包括女主角安及编剧杰克,他们遇到恐龙及当地土著的袭击,安发出的尖叫声换来金刚的回应。这只巨大无比的猩猩,连凶悍的恐龙也惧怕它几分,偏偏它却钟情于安。安其后将金刚由荒岛带回纽约,但却是它悲剧命运的开始。后来金刚被抓到了城市。为保护爱人同军队战斗,金刚为了带安再看一次她曾说过美丽的日出,爬上了帝国大厦,使自己陷入困境,与人类的飞机展开了最后决战。最后它摔下了帝国大厦,为自己的爱人谱写了最后的悲歌。

try-catch之一矮矬穷泡妞

java程序猿也会碰到难以琢磨的try-catch,请看下面的例子

    public static void main(String[] args) {
try {
System.out.println("Hello world");
} catch(IOException e) {
System.out.println("抓到一个IO 异常!");
}
}

这段代码可以打印出什么?

可能不少人会说,这个不是很简单嘛?打印出:

Hello world

其实它压根编译都不能通过

报错情况

Unreachable catch block for IOException. This exception is never thrown from the try statement body

简单的说,就是try里没有能抛出IOException异常的语句,catch该异常就通不过编译。

JSL-11.2.3里规定了

It is a compile-time error if a method or constructor body can throw some exception class E when E is a checked exception class and E is not a subclass of some class declared in the throws clause of the method or constructor.

It is a compile-time error if a lambda body can throw some exception class E when E is a checked exception class and E is not a subclass of some class declared in the throws clause of the function type targeted by the lambda expression.

It is a compile-time error if a class variable initializer (§8.3.2) or static initializer (§8.7) of a named class or interface can throw a checked exception class.

It is a compile-time error if an instance variable initializer (§8.3.2) or instance initializer (§8.6) of a named class can throw a checked exception class,
unless the named class has at least one explicitly declared constructor and the exception class or one of its superclasses is explicitly declared in the throws clause of each constructor. It is a compile-time error if a catch clause can catch checked exception class E1 and it is not the case that the try block corresponding to the catch clause can throw a checked exception class that is a subclass or superclass of E1,
unless E1 is Exception or a superclass of Exception. It is a compile-time error if a catch clause can catch an exception class E1 and a preceding catch clause of the immediately enclosing try statement can catch E1 or a superclass of E1.

根据上面所述,矮矬穷泡妞本身都被排除掉了,只有有一项特长,才能泡妞!

最简单的方法是抛出一个异常或者子异常

import java.io.IOException;
public class TryCatchException {
public static void main(String[] args) {
try {
System.out.println("Hello world");
throw new IOException();//或者子异常,如throw new FileNotFoundException();
} catch(IOException e) {
System.out.println("抓到一个IO 异常!");
}
}
}

try-catch之二高富帅泡妞

那来看看这个吧!打印什么?

public class TryCatchException {

    public static void main(String[] args) {
try {
System.out.println("hello world!");
} catch(Exception e) {
System.out.println("捕获到异常");
}
}
}

可能不少人会说,不是和上面的一样嘛!会报编译异常。

哈哈,你掉到坑里了!它打印

hello world!

不管与其相对应的try子句的内容是什么,捕获Exception或者Throwable的catch语句是ok的,这点jsl并没有说清楚。

总之,高富帅泡妞总是很超脱的,很多妞也愿意倒扑!

try-catch之三泡妞技可以继承吗?

我们来看看异常如何继承的吧

public interface FileNotFound {
void run() throws FileNotFoundException;
} public interface CloneNotSupported {
void run() throws CloneNotSupportedException;
} public class TryCatchException implements FileNotFound,CloneNotSupported {
public static void main(String[] args) {
TryCatchException e=new TryCatchException();
e.run();
}
@Override
public void run() {
System.out.println("Hello world");
}
}

上面的程序可以编译通过吗?不少人会说,不能通过编译!原因:TryCatchException继承了FileNotFound和CloneNotSupported的方法,同事必然继承它的异常,故必须捕获这两个异常。

你再次避过了正确答案!可以正确编译,且打印出结果。

一个方法可以抛出的受检查异常的集合时它所适用所有类型声明要抛出的受检查异常的交集,而不是合集。

小结:

矮矬穷就别想着捕获少女心了,想着一项特长吧,比如富!

高富帅可以无所顾忌,无往不利。

泡妞只能最新化的继承,不用太担心。

参考资料

【1】java解惑

猿类如何捕获少女心--难以琢磨的try-catch的更多相关文章

  1. 一个少女心满满的例子带你入门canvas

    https://blog.csdn.net/sunshine940326/article/details/76572850 本文首发于我的个人博客:http://cherryblog.site/ gi ...

  2. 花果山第一届猿类分级考试实录--Talk is cheap,Show me the code

    本故事纯属虚构,如有雷同,纯属巧合! 故事背景 悟空师徒4人取经回来后,因不耐收到管教,就回到了花果山,带领一帮猴子猴孙逍遥自在的过日子,奈何因在阎王殿里将生死薄中的猴子猴孙的名字都划去了,猴子猴孙是 ...

  3. C#钩子类 几乎捕获键盘鼠标所有事件

    using System; using System.Text; using System.Runtime.InteropServices; using System.Reflection; usin ...

  4. 用Exception类捕获所有异常的技术是怎么用的?

    3.用Exception类捕获所有异常  马克-to-win:注意,一个事实是:Exception类是所有其他异常类的父类,所以Exception类能捕获所有的异常.马克-to-win:问题是用Exc ...

  5. C++ Primer 学习笔记_63_重载运算符和转换 --转换和类类型【上】

    重载运算符和转换 --转换与类类型[上] 引言: 在前面我们提到过:能够用一个实參调用的位 unsignedchar 相同范围的值,即:0到255. 这个类能够捕获下溢和上溢错误,因此使用起来比内置u ...

  6. 创成汇丨投脑风暴·创心不止|路演日 第2期,寻IT创业者

    创成汇丨投脑风暴·创心不止|路演日 第2期   无畏荆棘之路的风雨 誓做浪潮之巅的勇者 你说,创业是一场孤注一掷的较量 你说,创新从来都是与过去battle 你还说,坚持总是比开始更让你难以琢磨 所以 ...

  7. mahout in Action2.2-聚类介绍-K-means聚类算法

    聚类介绍 本章包含 1 实战操作了解聚类 2.了解相似性概念 3 使用mahout执行一个简单的聚类实例 4.用于聚类的各种不同的距离測算方法 作为人类,我们倾向于与志同道合的人合作-"鸟的 ...

  8. PHP 面向对象编程和设计模式 (4/5) - 异常的定义、扩展及捕获

    PHP高级程序设计 学习笔记 2014.06.12 异常经常被用来处理一些在程序正常执行中遇到的各种类型的错误.比如做数据库链接时,你就要处理数据库连接失败的情况.使用异常可以提高我们程序的容错特性, ...

  9. 程序猿看小说还要去找TXT?自己动手爬一个TXT才是正确的打开方式

    前言 在贴吧看了个小说追了几天被删帖了,于是自己找书名,打算下载下来看,结果要么是需要充值,要么不提供下载.作为一个猿类,怎么能忍. 好在小说网站多入牛毛,有的采用js加载文字来防采集,有的用css图 ...

随机推荐

  1. charles(version4.2.1)抓包手机数据

    点击菜单栏的Proxy项,选择Proxy Settings. 设置HTTP Proxy的Port. 勾选透明代理Enable transparent HTTP proxying,也可不勾选. 设置代理 ...

  2. ssm执行流程

    SSM运行流程 1:服务器启动,创建springmvc的前端控制器DispatcherServlet,创建Spring容器对象. 加载spring-servlet.xml .applicationCo ...

  3. Ubuntu 16.04 安装 SVN-Client (RaabitVCS)

    1.添加源 sudo add-apt-repository ppa:rabbitvcs/ppa 2. 更新源 sudo apt-get update 3.安装依赖库 sudo apt-get inst ...

  4. React 练习项目,仿简书博客写作平台

    Introduction 技术栈:react + redux + react-router + express + Nginx 练习点: redux 连接 react-router 路由跳转 scss ...

  5. Nginx 1.15.5: 405 Not Allowed

    0x00 事件 在做一个业务跳转时,遇到这个错误 405 Not Allowed,找了挺多资料,多数解决方案是让在 nginx 配置文件中直接添加 error_page 405 =200 $uri; ...

  6. scrapy xpath用法

    一.实验环境 1.Windows7x64_SP1 2.anaconda3 + python3.7.3(anaconda集成,不需单独安装) 3.scrapy1.6.0 二.用法举例 1.开启scrap ...

  7. 重学计算机网络(二) - 曾记否,查IP地址

    先献上几个梗 1.1.1.1 不是测试用的,原来一直没分配,现在被用来做一个DNS了,宣传是比谷歌等公司的dns服务 更保护用户隐私. IP地址255.255.255.255,代表有限广播,它的目标是 ...

  8. Visual Studio 2015&2017 key

    Visual Studio 2015 key Key : HMGNV-WCYXV-X7G9W-YCX63-B98R2 Visual Studio Enterprise 2015 Key :HM6NR- ...

  9. Tomcat源码分析 (九)----- HTTP请求处理过程(二)

    我们接着上一篇文章的容器处理来讲,当postParseRequest方法返回true时,则由容器继续处理,在service方法中有connector.getService().getContainer ...

  10. Python 标识符说明

    在Python中,标识符有字母.数字.下划线组成 所有标识符都可以包括英文.数字.下划线,但不能以数字开头 Python标识符区分大小写 ※以下划线开头的标识符有特殊含义. 例如:以单下划线开头(_t ...