[#0x001D] first catch

本文深入探讨了Java中异常处理机制的工作原理,特别是不同catch块之间的执行顺序和异常类型的匹配规则。通过具体示例展示了如何避免基类异常捕获对子类异常捕获产生的屏蔽效应。

  try块里有异常抛出时,程序会进入第一个异常类型匹配的catch块执行,其他的catch块不执行。

  注意这里说的“第一个匹配的异常类型”。我们知道,所有的异常类型都是extends Exception,所以catch (Exception e)可以catch所有的异常;也就是说,这里说的“异常类型匹配”包括向上转型。

  同时,对基类异常的catch块会屏蔽其后的对其导出类异常的catch块,而且这个屏蔽只与catch块代码的位置有关系,如:

class MyException extends Exception 
{
}

public class ExceptionTest
{
	public static void main(String[] args)
	{
		try 
		{
			throw new MyException();
		}
		catch (Exception e)
		{
			System.out.println("Catch an Exception: " + e.getClass().toString());
		}
		catch (MyException me)
		{
			System.out.println("Catch a MyException: " + me.getClass().toString());
		}	
	}
}

//output: Compiling Error
/* 已捕捉到异常MyException*/

//when comment or delete the 'catch (MyException me)' block
//output:
/* Catch an Exception: class MyException*/

  这里,catch (Exception e)就屏蔽了catch (MyException me)。但是,如果我们把catch (MyException me)置于catch (Exception e)之上,就不会有屏蔽。如:

class MyException extends Exception 
{
}

public class ExceptionTest
{
	public static void main(String[] args)
	{
		try 
		{
			throw new MyException();
		}
		catch (MyException e)
		{
			System.out.println("Catch a MyException: " + e.getClass().toString());
		}
		catch (Exception e)
		{
			System.out.println("Catch an Exception: " + e.getClass().toString());
		}	
	}
}

//output:
/* Catch a MyException: class MyException*/
07:08:33: Debugging D:\sjkshqttest3\untitled0\build\Desktop_Qt_6_8_3_MSVC2022_64bit-Debug\RocketMQVisualizer.exe "-qmljsdebugger=port:10547,block,services:DebugMessages,QmlDebugger,V8Debugger,QmlInspector,DebugTranslation" ... Exception at 0x7ffc55a685ea, code: 0xe06d7363: C++ exception, flags=0x81 (first chance) in DWrite Exception at 0x7ffc55a685ea, code: 0xe06d7363: C++ exception, flags=0x81 (first chance) in DWrite Exception at 0x7ffc55a685ea, code: 0xe06d7363: C++ exception, flags=0x81 (first chance) in DWrite Exception at 0x7ffc55a685ea, code: 0xe06d7363: C++ exception, flags=0x81 (first chance) in DWrite Exception at 0x7ffc55a685ea, code: 0xe06d7363: C++ exception, flags=0x81 (first chance) in DWrite Exception at 0x7ffc55a685ea, code: 0xe06d7363: C++ exception, flags=0x81 (first chance) in DWrite QML Debugger: Ignoring "-qmljsdebugger=port:10547,block,services:DebugMessages,QmlDebugger,V8Debugger,QmlInspector,DebugTranslation". Debugging has not been enabled. [328168:330736:0703/070839.477:ERROR:gpu_channel_manager.cc(959)] Failed to create GLES3 context, fallback to GLES2. [328168:330736:0703/070839.478:ERROR:gpu_channel_manager.cc(970)] ContextResult::kFatalFailure: Failed to create shared context for virtualization. [328168:318816:0703/070841.179:INFO:permission_manager_qt.cpp(85)] Unexpected unsupported Blink permission type: 10 [328168:318816:0703/070841.180:INFO:permission_manager_qt.cpp(85)] Unexpected unsupported Blink permission type: 10 js: 获取失败: SyntaxError: "[object Object]" is not valid JSON js: 获取失败: SyntaxError: "[object Object]" is not valid JSON js: 获取失败: SyntaxError: "[object Object]" is not valid JSON js: 获取失败: SyntaxError: "[object Object]" is not valid JSON js: 获取失败: SyntaxError: "[object Object]" is not valid JSON js: 获取失败: SyntaxError: "[object Object]" is not valid JSON js: 获取失败: SyntaxError: "[object Object]" is not valid JSON js: 获取失败: SyntaxError: "[object Object]" is not valid JSON
07-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值