使用try/catch方法获取一个参数,该参数将从java中的方法中产生特定的输出



我使用try/catch编写了这段代码,并希望它输出以下内容:

A null pointer exception occurred
In the finally block
The length of the string is 1
A number format exception occurred
In the finally block
A null pointer exception occurred
Some type of exception occurred
In the finally block
The length of the string is 1
The value is 5

但由于某种原因,当我调用时,我试图从main调用它,我得到了一个错误,所以我的问题是如何正确地从main调用方法以获得上面的输出?

这是我写的方法:

public static void problem2(String s) {
    try {
        int stringLen = s.length();
        System.out.println("The length of the string is " + stringLen);
        int i = Integer.parseInt(s);
        System.out.println("The value is " + i);
    } catch (NumberFormatException e1) {
        System.out.println("A number format exception occurred");
    } catch (NullPointerException e2) {
        System.out.println("A null pointer exception occurred");
    } catch (Exception e3) {
        System.out.println("Some type of exception occurred");
    } finally {
        System.out.println("In the finally block");
    }
}

假设您将此静态方法放置在类"TryCatchExample"中,则以下内容将生成示例1、2和4的输出。由于您捕获了NullPointerException,代码将不会从一个catch块传递到下一个。此外,你为什么要这样做?您捕捉到了更具体的Exception,它可能允许向用户提供一些特定的反馈。更普遍的Exception信息量较小,许多人认为这是一种糟糕的方法。

public static void main(String[] args)
{
    TryCatchExample.problem2(null);  // prints NPE
    TryCatchExample.problem2("A"); // prints numberformat
    // no example for both numberformat and npe
    TryCatchExample.problem2("5"); // prints lengths & value 5
}

我刚刚运行了您的代码,它似乎运行得很好

public class UnknownError {
public static void main(String[] args) {
    problem2("hello");
}
 public static void problem2(String s) {
      try {
      int stringLen = s.length();
      System.out.println("The length of the string is " + stringLen);
      int i = Integer.parseInt(s);
      System.out.println("The value is " + i);
      } catch (NumberFormatException e1) {
      System.out.println("A number format exception occurred");
      } catch (NullPointerException e2) {
      System.out.println("A null pointer exception occurred");
      } catch (Exception e3) {
      System.out.println("Some type of exception occurred");
      } finally {
      System.out.println("In the finally block");
        }
 }

}

您调用的方法与此不同吗?

该方法具有以下签名,并且有三种可能性。我希望您了解以下可能性,然后用适当的参数值调用您的方法,或者在代码中进行更改,以获得您想要的输出。对你来说,学习会比我或你写下解决方案代码更好。

public static void problem2(String s)

第一种可能性

如果参数s为Null,则int stringLen = s.length();将导致NullPointerException,因为您正试图从从未创建的对象(Null)中获取length()。所以现在代码流将到达下面的块。

catch (NullPointerException e2) {
  System.out.println("A null pointer exception occurred");
  }

所以打印的下一行是出现空指针异常

下一个finally将被调用,打印的下一行将在finally块中

第二种可能性

如果参数不为null。

int stringLen = s.length();
System.out.println("The length of the string is " + stringLen);

上面的代码将打印字符串的长度为1(如果字符串中只有一个字符,则为1)。

在情况s中,包含与Integer不对应的字符串,在这种情况下为

int i = Integer.parseInt(s);

此代码将抛出NumberFormatException,流将到达以下代码。

catch (NumberFormatException e1) {
  System.out.println("A number format exception occurred");
}

这将打印发生数字格式异常

再次执行finally块,并在finally块中打印

第三种可能性

如果您的字符串参数不为空,并且包含与有效整数对应的字符,则

int stringLen = s.length();
System.out.println("The length of the string is " + stringLen);
int i = Integer.parseInt(s);
System.out.println("The value is " + i);

此代码将导致打印字符串的长度为**,后跟**值为1,后跟从finally块打印的字符串,在finally块中

最新更新