finalexception.java

来自「Java程序设计技巧与开发实例附书源代码。」· Java 代码 · 共 55 行

JAVA
55
字号

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();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == quit) {
            System.exit(0);
        }
        if (e.getSource() == enter) {
            display.setText("");
            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 + -
显示快捷键?