在多个类

时间:2016-03-31 18:43:16

标签: java

您好我处理自定义异常时遇到问题。 当用户没有输入名字和姓氏时,我的异常被抛出以下是每个类的代码:

public class EmployeeException extends Exception{
    public EmployeeException(String message){
        super("invalid");
    }
}   

public abstract class Employee{
    private String employeeName;
    private String departmentName;

    public Employee(String employeeName, String departmentName)throws EmployeeException{
        String [] tokens= employeeName.split(" ");

        if(tokens.length!=2)
            throw new EmployeeException("please use space to separate first and last name");

        this.employeeName=employeeName;
        this.departmentName=departmentName;
    }

这是我未获得未报告异常错误的驱动程序。该指令表明司机不需要被告知例外情况

    import java.util.Scanner;
public class CompanyDriver{
    public static void main(String[] args){
        Scanner kb= new Scanner(System.in);
        String name;
        String compStreet;
        String compCity;
        String compState;
        String compZip;

        System.out.print("What is the name of the company?");
        name=kb.nextLine();
        System.out.print("What is the address of the company?");
        System.out.println();
        System.out.print("Street:"); 
        compStreet=kb.nextLine();
        System.out.print("City:");
        compCity=kb.nextLine();
        System.out.print("State:");
        compState=kb.nextLine();
        System.out.print("Zip:");
        compZip=kb.nextLine();
        System.out.println();
        Address addr =new Address(compStreet, compCity, compState, compZip);

        Company comp=new Company(name, addr);
        System.out.println(comp);
        comp.addEmployee();
        comp.addEmployee();
        comp.weeklyPayReport();

基本上我的程序应该在用户没有为员工姓名输入2个令牌时抛出我的自定义异常

0 个答案:

没有答案