?? exception6.java
字號:
//處理多種異常***(輸入姓名、工資值)
import javax.swing.JOptionPane;
class mathException extends Exception
{
mathException()
{
System.out.println("輸入數(shù)據(jù)不正確");
}
}
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();//假如沒有輸入名字就"拋出"一個(gè)Exception異常
pay=Integer.parseInt(JOptionPane.showInputDialog("請輸入您的月工資"));
if(pay<0) throw new mathException();//假如輸入的月工資數(shù)小于零,就會(huì)"拋出"自定義異常mathException
}
catch(Exception e) //捕獲Exception異常
{
System.out.println(e);
System.exit(0);
}
}
public static void main(String args[])
{
try
{
for(int i=1;;i++) //沒有給出循環(huán)次數(shù)限制
{
inputdata();
System.out.println(name+"的年薪是"+pay*12);
}
}
catch(mathException pt) //捕獲自定義異常
{
System.out.println(pt);
System.exit(0);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -