Java 如果使用Long.parseLong()解析失败,如何引发异常?
我想写一个堆栈计算器 下面是我处理Java 如果使用Long.parseLong()解析失败,如何引发异常?,java,exception-handling,try-catch,Java,Exception Handling,Try Catch,我想写一个堆栈计算器 下面是我处理push命令的代码部分。我只想推送整数,因此我必须清除任何无效字符串,如foobar(无法解析为整数)或9999999999(超出整数范围) strings在我的代码中是一个字符串表,其中包含已被白色字符分割的POP或PUSH等命令、数字和随机杂波 主要问题: 我很难为long parseNumber=long.parseLong(strings[I])抛出异常-我不知道如何处理这种情况,当字符串[I]不能被解析为长的并随后被解析为整数时 while (i &l
push
命令的代码部分。我只想推送整数,因此我必须清除任何无效字符串,如foobar
(无法解析为整数)或9999999999
(超出整数范围)
strings
在我的代码中是一个字符串表,其中包含已被白色字符分割的POP
或PUSH
等命令、数字和随机杂波
主要问题:
我很难为long parseNumber=long.parseLong(strings[I])抛出异常代码>-我不知道如何处理这种情况,当字符串[I]
不能被解析为长的并随后被解析为整数时
while (i < strings.length) {
try {
if (strings[i].equals("PUSH")) {
// PUSH
i++;
if (strings[i].length() > 10)
throw new OverflowException(strings[i]);
// How to throw an exception when it is not possible to parse
// the string?
long parseNumber = Long.parseLong(strings[i]);
if (parseNumber > Integer.MAX_VALUE)
throw new OverflowException(strings[i]);
if (parseNumber < Integer.MIN_VALUE)
throw new UnderflowException(strings[i]);
number = (int)parseNumber;
stack.push(number);
}
// Some options like POP, ADD, etc. are omitted here
// because they are of little importance.
}
catch (InvalidInputException e)
System.out.println(e.getMessage());
catch (OverflowException e)
System.out.println(e.getMessage());
catch (UnderflowException e)
System.out.println(e.getMessage());
finally {
i++;
continue;
}
}
while(i10)
抛出新的OverflowException(字符串[i]);
//无法解析时如何引发异常
//绳子?
long parseNumber=long.parseLong(字符串[i]);
if(parseNumber>Integer.MAX_值)
抛出新的OverflowException(字符串[i]);
if(parseNumber不用担心Long.parseLong()
如果得到的不是Number
,Long.parseLong(String str)
如果由于任何原因无法解析字符串,则抛出NumberFormatException
。您可以通过为try添加catch块来捕获相同的内容,如下所示:
while (i < strings.length) {
try {
if (strings[i].equals("PUSH")) {
// PUSH
i++;
if (strings[i].length() > 10)
throw new OverflowException(strings[i]);
// How to throw an exception when it is not possible to parse
// the string?
long parseNumber = Long.parseLong(strings[i]);
if (parseNumber > Integer.MAX_VALUE)
throw new OverflowException(strings[i]);
if (parseNumber < Integer.MIN_VALUE)
throw new UnderflowException(strings[i]);
number = (int)parseNumber;
stack.push(number);
}
// Some options like POP, ADD, etc. are omitted here
// because they are of little importance.
}
catch (InvalidInputException e)
System.out.println(e.getMessage());
catch (OverflowException e)
System.out.println(e.getMessage());
catch (UnderflowException e)
System.out.println(e.getMessage());
finally {
i++;
continue;
}
}
catch ( NumberFormatException e) {
System.out.println(e.getMessage());
}
在阅读了您的评论和答案后,我能够想出这样一个解决方案(此代码嵌入到外部try catch
)
if(字符串[i].equals(“PUSH”)){
//推
i++;
if(字符串[i].length()>10){
抛出新的OverflowException(字符串[i]);
}
试一试{
parseNumber=Long.parseLong(字符串[i]);
if(parseNumber>Integer.MAX_值){
抛出新的OverflowException(字符串[i]);
}
if(parseNumber
NumberFormatException
Long.parseLong在无法解析字符串时抛出NumberFormatException。您可以将其包装在try-catch中,以捕获字符串不长的情况。@nullPointer,好的,这样我就可以为long parseNumber=long.parseLong(strings[I])创建另一个try-catch代码>如果我想为了练习而添加我自己的异常,对吗?@Mateusz这是正确的