如何捕获parseInt抛出的NumberFormatException

use*_*780 4 java parseint numberformatexception

我坚持这个问题〜:

方法Integer.parseInt将数字字符串转换为它表示的int数.

public static int parseInt (String s)
             throws NumberFormatException
Run Code Online (Sandbox Code Playgroud)

解释如何捕获parseInt抛出的异常,并用代码示例说明您的答案.

到目前为止,我的想法是:

try
{
   System.out.println("incorrect");

}

catch (NumberFormatException n)
{
   System.out.println("WRONG! This is not a number");
}
Run Code Online (Sandbox Code Playgroud)

我觉得这个答案是错误的,因为上面的代码只会在main方法中使用,我不知道如何在下面捕获异常:

public static int parseInt (String s)
             throws NumberFormatException.
Run Code Online (Sandbox Code Playgroud)

我通过java API查看了parseInt方法,并通过各种研究查看并尝试了代码,但是由于我缺乏理解,我仍然在努力做到这一点.

如果有人能帮助我,我将不胜感激,提前谢谢.

NPE*_*NPE 5

由于这是功课,我只是给你一个提示:你要调用的方法,并围绕与适当的呼叫try- catch块.