为什么尝试捕获我的代码收到错误?

Why try catch on my code received error?

java 编程和尝试 JOptionPane 函数的新功能。在我的代码的最后一部分,当用户输入有关年龄的字符或字符串时,我会进行验证并显示无效消息。

这是我的代码:

package activity1;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Activity1 {

    public static void main(String[] args) {

        JOptionPane.showMessageDialog(null, "JOptionPane Activity", "Sample Message Box", JOptionPane.INFORMATION_MESSAGE);

        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        DateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date();
        Calendar time = Calendar.getInstance();
        ImageIcon icon = new ImageIcon("D:\java\calendar.jpg");
        JOptionPane.showMessageDialog(null, "today is " + dateFormat.format(date) + "\n" +"and the time is " + timeFormat.format(time.getTime()), "TIME BOX", JOptionPane.INFORMATION_MESSAGE, icon);

        ImageIcon icon2 = new ImageIcon("D:\java\question.jpg");
        String[] options = new String[] {"Yes", "No", "Maybe"};
        int selection = JOptionPane.showOptionDialog(null, "Do you want to continue?", "CONFIRMATION MESSAGE", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, icon2, options, options[0]);

        if(selection==0) {
            JOptionPane.showMessageDialog(null, "You've chosen to continue...", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
        else if(selection==2) {
            JOptionPane.showMessageDialog(null, "Indecision means yes so we will proceed...", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showMessageDialog(null, "You've cancelled the operation,,,", "Message", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        String name;
        name = JOptionPane.showInputDialog(null, "Enter your name:", "Sample Inputbox", JOptionPane.WARNING_MESSAGE);
        JOptionPane.showMessageDialog(null, "Hello " + name);

        String age;
        age = JOptionPane.showInputDialog(null, "How old are you?", "Sample Inputbox", JOptionPane.WARNING_MESSAGE);
        int year, born;
        year = Calendar.getInstance().get(Calendar.YEAR);
        born = year - (Integer.parseInt(age));

        try {
            Integer.parseInt(age);
        } 
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invalid input...Goodbye!", "Sample Message Box", JOptionPane.INFORMATION_MESSAGE);
            return;
        } 

        JOptionPane.showMessageDialog(null, "So you were born on year " + born, "Sample Message Box", JOptionPane.QUESTION_MESSAGE);
    }

}

这是我输入"a"年龄时的错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at activity1.Activity1.main(Activity1.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)

错误是因为语句

 born = year - (Integer.parseInt(age));

这应该在 try - catch 块内

try {
   born = year - (Integer.parseInt(age));
   ...
} catch {
   ...
}

您在 try catch 块外解析字符串:

  born = year - (Integer.parseInt(age));

也将其移动到块中。

    try {
        born = year - (Integer.parseInt(age));
    } 
    catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid input...Goodbye!", "Sample Message Box", JOptionPane.INFORMATION_MESSAGE);
        return;
    } 

您错误地使用了 try-catch。

born = year - (Integer.parseInt(age)); 行不在 try-catch 块内,因此触发异常。

此外,try-catch 中的行不存储转换结果。

注意:在实际应用中尽量不要使用绝对路径。存储与您的应用程序相关的数据,因此当文件位置更改时您不会收到 FileNotFound 错误。