throwingexceptions.java

来自「java程序设计语言源代码」· Java 代码 · 共 45 行

JAVA
45
字号
//Example 4 of Chapter 4

import java.io.*;
public class ThrowingExceptions
{
	public static void main( String args[] )
	{
		try {
			throw1();
		}
		catch ( EOFException eofe )
		{
			System.err.println( eofe.getMessage() + "\n" );
		}
		catch ( IOException ioe )
		{
			System.err.println( ioe.getMessage() + "\n" );
		}
		catch ( Exception e )
		{
			System.err.println( e.getMessage() + "\n" );
			e.printStackTrace();
		}
	}
	
	public static void throw1() throws Exception
	{
		throw2();
	}
	
	public static void throw2() throws Exception
	{
		try{
			System.out.println( "方法二" );
			throw new Exception( "在方法二中抛出" );
		}
		catch(RuntimeException re)
		{
			System.err.println( "在方法二的捕获中抛出" );
		}
		finally{
			System.err.println( "finally总是执行的" );
		}
	}
}

⌨️ 快捷键说明

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