⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useexthrow.java

📁 java基础教程以及代码 java base document and code
💻 JAVA
字号:
// 演示如何  抛出异常   
// 一般用于:某个地方不想处理,想让它的上一级的方法去处理的时候

// 执行结果:
// C:\java>java  UseExThrow
// java.lang.Exception: Ex Self Throw in mothod C   caught in main
// java.lang.Exception: Ex Self Throw in mothod C
//        at UseExThrow.MethodC(UseExThrow.java:26)
//        at UseExThrow.MethodB(UseExThrow.java:21)
//        at UseExThrow.MethodA(UseExThrow.java:17)
//        at UseExThrow.main(UseExThrow.java:7)

public class UseExThrow
{
	public static void main(String [] args)
	{
		try 
		{
			MethodA();  
		}
		catch (Exception e)
		{
			System.out.println(e.toString()+"   caught in main");
			e.printStackTrace();
		}
	}
	public static void MethodA() throws Exception
	{
		MethodB();
	}
	public static void MethodB() throws Exception
	{
		MethodC();
	}
	public static void MethodC() throws Exception
	{
		// 抛出异常
		throw new Exception("Ex Self Throw in mothod C");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -