Java小白踩坑录 - 难以琢磨的try-catch

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

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

img

报错情况

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 的方法,同时必然继承它的异常,故必须捕获这两个异常。

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

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊的牧码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值