finalexception.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 61 行

JAVA
61
字号
import java.awt.*;
import java.awt.event.*;

public class FinalException extends Frame implements ActionListener
{
	public Button quit			=	new Button("quit"), enter	=	new Button("go");
	public ShortTextField text	=	new ShortTextField();
	public TextArea display		=	new TextArea(3, 25);

	public FinalException()
	{
		super("Exceptions with finally");
		quit.addActionListener(this);
		enter.addActionListener(this);
		Panel input	=	new Panel();
		input.setLayout(new BorderLayout());
		input.add("West", enter);
		input.add("Center", text);
		input.add("East", quit);
		setLayout(new BorderLayout());
		add("Center", display);
		add("North", input);
		validate();
		pack();
		setSize(600, 250);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == quit)
			System.exit(0);
		if (e.getSource() == enter)
		{
			display.setText("");
			text.setText("");
			text.requestFocus();
			display.append("line before try-catch-finally block\n");
			try
			{
				display.append("line before getshorttext()\n");
				display.append(text.getShortText() + "\n");
				display.append("line after getshorttext()\n");
			}
			catch (LongStringException se)
			{
				display.append("exception occured: " + se.getMessage() + "\n");
			}
			finally
			{
				display.append("finally block executes\n");
				display.append("line after try-catch-finally block\n");
			}
		}
	}	
	public static void main(String[] args)
	{
		FinalException fe	=	new	FinalException();
	}
}

⌨️ 快捷键说明

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