所以在处理多异常时应注意认真设计各catch块的排列顺序。一般地处理较具体和较常见的异常的catch块应放在前面,而可以与多种异常相匹配的catch块应放在较后的位置。
/*尝试用户运行错误的异常处理:
问题是这样的,当输入的用户工资初值少于800则是错误的,当然工资的变化如果超过20%,则也是错误的
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class UserExceptionApplet extends Applet implements ActionListener{
Label prompt1=new Label("请输入雇员姓名和工资初值:");
Label prompt2=new Label("请输入欲修改的工资");
TextField name,isal,nsal;
String msg;
Employee Emp;
Button okBtn=new Button("OK");
Button cancelBtn=new Button("Cancel");
public void init(){
name=new TextField(5);
isal=new TextField(5);
nsal=new TextField(5);
add(prompt1);
add(name);
add(isal);
add(prompt2);
add(nsal);
add(okBtn);
okBtn.addActionListener(this);
cancelBtn.addActionListener(this);
add(cancelBtn);
}
public void paint(Graphics g){
g.drawString(msg,0,80);
}
public void CreateEmp(String empName,double sa){
try{
Emp=new Employee(empName,sa);
msg=new String(Emp.toString());
}
catch(IllegalSalaryException ise){
msg=new String(ise.toString());
}
}
public void ChangeEmpSal(double changeSal){
try{
Emp.setEmpSalary(changeSal);
msg=new String(Emp.toString());
}
catch(IllegalSalaryException illSal){
msg=new String(illSal.toString());
}
catch(IllegalSalaryChangeException illSalChange){
msg=new String(Emp.toString()+illSalChange.toString());
}
}
public void actionPerformed(ActionEvent e){
String empName;
double empSal,changeSal;
Object obj=e.getSource();
if(obj==okBtn){
empName=new String(name.getText());
if(empName==null){
msg=new String("请先输入雇员姓名工资并创建之");
}
if(nsal.getText()==null){
empSal=Double.valueOf(isal.getText()).doubleValue();
CreateEmp(empName,empSal);
}
else{
changeSal=Double.valueOf(nsal.getText()).doubleValue();
ChangeEmpSal(changeSal);
}
}
if(obj==cancelBtn){
naem.setText("");
isal.setText("");
nsal.setText("");
}
repaint();
}
}
class Employee{
String m_EmpName;
double m_EmpSalary;
Employee(String name,double initsalary)throws IllegalSalaryException{
m_EmpName=name;//看这里有问题没,参考代码为m_EmpName=new String(name);
if(initsalary<800){
throw(new IllegalSalaryException(this,initsalary));//throw语句
}
m_EmpSalary=initsalary;
}
public String getEmpName(){
return m_EmpName;
}
public double getEmpSalary(){
return m_EmpSalary;
}
public boolean setEmpSalary(double newSal) throws IllegalSalaryException,IllegalSalaryChangeException{
if(newSal<800)
throw(new IllegalSalaryException(this,newSal));
else if(getEmpSalary()==0.0){
m_EmpSalary=newSal;
return true;
}
else if(Math.abs(newSal-getEmpSalary())/getEmpSalary()>=0.2)
throw(new IllegalSalaryChangeException(this,newSal-getEmpSalary()));
else{
m_EmpSalary=newSal;
return true;
}
}
public String toString(){
String s;
s="姓名:"+m_EmpName+"工资: "+m_EmpSalary;
return s;
}
}
class IllegalSalaryException extends Exception{
private Employee m_ConcernedEmp;
private double m_IllegalSalary;
IllegalSalaryException(Employee emp,double isal){
super("工资低于最低工资");
m_ConcernedEmp=emp;
m_IllegalSalary=isal;
}
public String toString(){
String s;
s="为雇员提供的工资不合法:雇员:"+m_ConcernedEmp.getEmpName()+"非法工资:"+m_IllegalSalary+"低于最低工资数额800元";
return s;
}
}
class IllegalSalaryChangeException extends Exception{
private Employee m_ConcernedEmp;
private double m_IllegalSalaryChange;
IllegalSalaryChangeException(Employee emp,double csal){
super("工资变动太大");
m_ConcernedEmp=emp;
m_IllegalSalaryChange=csal;
}
public String toString(){
String s;
s="为雇员提供的工资变动不合法:雇员:"+m_ConcernedEmp.getEmpName()+"非法变动工资变化:"+m_IllegalSalaryChange+"高于原工资的20%";
return s;
}
}