Java 异常处理变量未初始化

标签 java variables exception

我试图捕获插入文本且程序必须解析Int 的异常。 编译后,它说变量 num 可能尚未初始化。 (31号线) 我无法弄清楚为什么它会这样做。

代码如下。提前致谢。

// Java packages
import javax.swing.JOptionPane; 
// program uses JOptionPane

class Help2Gui {
    public static void main(String args[]) {

        // variable declaration
        String choice;
        int num;

        // read in first number from user as a string
        choice =
            JOptionPane
                .showInputDialog("Help on: \n \t 1. class \n \t 2. object \n \t 3. method \n \t 4. variable \n \t 5. constructor \n \t 6. Quit \n Enter a number from the list above.");

        if (choice == null) {
            System.exit(0);
        }

        do { // begin a loop to display initial choice, repeating until 6 is
             // entered

            // convert numbers from type String to type int
            try {
                num = Integer.parseInt(choice);
            }

            catch (NumberFormatException nfe) {

            }

            switch (num) { // display result for each item entered by user
            case 1:
                JOptionPane.showMessageDialog(null,
                    "\n A class is a definition of an object.",
                    "Java Help System", JOptionPane.PLAIN_MESSAGE);
                break;

            case 2:
                JOptionPane
                    .showMessageDialog(
                        null,
                        "The switch: \n \n switch (expression) { \n case constant: \n statement sequence \n break \n // ... \n } ; ",
                        "Java Help System", JOptionPane.QUESTION_MESSAGE);
                break;

            case 3:
                JOptionPane
                    .showMessageDialog(
                        null,
                        "The for: \n \n for(init; condition; iteration) \n statement;",
                        "Java Help System", JOptionPane.INFORMATION_MESSAGE);
                break;

            case 4:
                JOptionPane.showMessageDialog(null,
                    "The while: \n \n while(condition) statement;",
                    "Java Help System", JOptionPane.WARNING_MESSAGE);
                break;

            case 5:
                JOptionPane
                    .showMessageDialog(
                        null,
                        "The do-while: \n \n do { \n statement; \n } while (condition);",
                        "Java Help System", JOptionPane.ERROR_MESSAGE);
                break;

            case 6:
                System.exit(0);
                break;

            default:
                JOptionPane.showMessageDialog(null,
                    "Enter a number from 1 to 5 or 6 to Quit",
                    "Java Help System", JOptionPane.ERROR_MESSAGE);
            }

            // read in first number from user as a string
            choice =
                JOptionPane
                    .showInputDialog("Help on: \n \t 1. if \n \t 2. switch \n \t 3. for \n \t 4. while \n \t 5. do-while \n \t 6. Quit \n Enter a number from the list above.");
            try {
                // attempt to convert the String to an int
                num = Integer.parseInt(choice);

            }
            catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null,
                    "Enter a number from 1 to 5 or 6 to Quit",
                    "Java Help System", JOptionPane.ERROR_MESSAGE);
            }
            // convert numbers from type String to type int

        }
        while (num != 6); // end of do-while loop.

        System.exit(0); // terminate application with window

    } // end method main

} // end class Help2Gui

最佳答案

如果异常被抛出并被捕获,则变量num不会被初始化

num = Integer.parseInt( choice );

然后就会出现问题:

switch (num) { //what is num in here?

要解决这个问题 - 您可以为这些情况指定一个默认值(在异常处理程序中,或在 try block 之前,因此对 num 的赋值将覆盖默认值),或终止抛出异常时的方法。
恕我直言,最简单的解决方案(尽管并不总是合适)是在声明它时初始化num,例如:

int num = MY_DEFAULT_VALUE;

关于Java 异常处理变量未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10835068/

相关文章:

Javascript 变量显示 NaN

python - 自动更新变量

c - 将信息放入列表中

java - org.apache.axis2.AxisFault :Transport error: 407 Error:Proxy Authentication Required

java - JPA。如何子类化现有实体并保留其 ID?

Java:无法使用 getConstructor() 捕获异常

c# - 使用 context.SaveChanges() == 0 测试操作是否成功

Python 异常处理 - 最佳实践

java - 如何定义以连字符命名的任务?

java rmi 服务器主机名