📄 sumexception.java
字号:
class NumberRangeException extends Exception
{
public NumberRangeException()
{
super();
}
public NumberRangeException(String msg)
{
super(msg);
}
}
public class SumException
{
public static String sum(String str1, String str2) throws NumberRangeException
{
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
if( (int1 < 0) || (int1 > 100) || (int2 < 0) || (int2 > 100) )
{
throw(new NumberRangeException("输入的数字超出范围!") );
}
return Integer.toString(int1 + int2);
}
public static void main(String[] args)
{
try
{
String sum = sum(args[0],args[1]);
System.out.println("两数相加结果:" + sum);
}
catch(NumberRangeException e)
{
System.out.println("错误:" + e.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -