📄 exception6.java
字号:
//处理多种异常***(输入姓名、工资值)
import javax.swing.JOptionPane;
class mathException extends Exception
{
mathException()
{
System.out.println("输入数据不正确");
}
}
public class Exception6
{
public static String name;
public static int pay;
public static void inputdata() throws mathException
{
try
{
name=JOptionPane.showInputDialog("请输入您的姓名");
if(name.equals("")) throw new Exception();//假如没有输入名字就"抛出"一个Exception异常
pay=Integer.parseInt(JOptionPane.showInputDialog("请输入您的月工资"));
if(pay<0) throw new mathException();//假如输入的月工资数小于零,就会"抛出"自定义异常mathException
}
catch(Exception e) //捕获Exception异常
{
System.out.println(e);
System.exit(0);
}
}
public static void main(String args[])
{
try
{
for(int i=1;;i++) //没有给出循环次数限制
{
inputdata();
System.out.println(name+"的年薪是"+pay*12);
}
}
catch(mathException pt) //捕获自定义异常
{
System.out.println(pt);
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -