2012-11-19 68 views
1

这是我的超时时间,除非销售目标不符合要求打印消息。如何实现自定义异常?

public class OverTimeException extends Exception { 

     int salesTarget; 
     public OverTimeException(int s) { 
      salesTarget = s; 
     } 
     String myPrint() 
     { 

      return "exception caught because sales target is " + salesTarget; 


     } 
    } 

这是我想要使用的例外salesTarget method.My的问题是如何实现异常到salesTarget方法秘书类?谢谢

public class Clerk extends Admin implements ClerkInterface { 

    final double payRate = 13.00; 
    final int salesTargetNum = 46; 


    //Member variables 
    private double unitSold; 

    //Constructor 
    public Clerk(String inName, String inId, double inHoursWorked, 
      double intkPay, double inAdmin_quota,double inUnitSold) { 
     super(inName, inId, inHoursWorked, intkPay, inAdmin_quota); 

     setUnitSold(inUnitSold); 

    } 


    //setUnitsSold method 
    private void setUnitSold(double inUnitSold) { 
     unitSold = inUnitSold; 

    } 


    //getUnitsSold 
    public double getUnitsSold(){ 

     return unitSold; 
    } 





    //toString method 
    public String toString() 
    { 
     return " " + super.toString() + "Admin Quota " + getAdmin_quota() + "Units Sold" + getUnitsSold(); 


    } 


    //method to confirm sales targets are made from ClerkInterface. 
    public void salesTarget() { 

     if(salesTargetNum >= unitSold){ 

      System.out.println("Clerk has not meet sales target"); 
     } 
     else 
     { 
      System.out.println("Clerk has meet sales target"); 
     } 

    } 





    }//end Clerk 

回答

4

如果你想扔例外,简单地做

throw new OverTimeException(salesTargetNum); 
+0

我试过这个,但它表明salesTarget不能解析为局部变量,任何想法?谢谢 –

+0

抛出异常时要引用的成员是salesTargetNum,而不是salesTarget。 – ScoPi

+0

对不起,我的意思是说salesTargetNum是给这个错误。 –

0

您需要

  1. 声明异常的方法中签名方法所需

  • 掷例如

    public void salesTarget() throws OvertimeException{ 
        ... 
        throws new OvertimeException(s); 
    } 
    

    请注意,您不需要在方法签名中声明unchecked exceptions

  • 1
    if(salesTargetNum >= unitSold){ 
        throw new OverTimeException(salesTarget);   
    } else { 
        System.out.println("Clerk has meet sales target"); 
    } 
    

    而且注意,您需要一个toString方法在OverTimeException类而不是toPrint方法。

    public class OverTimeException extends Exception { 
        int salesTarget; 
        public OverTimeException(int s) { 
         salesTarget = s; 
        } 
        public String toString() { // Should be `toString` 
         return "exception caught because sales target is " + salesTarget; 
        } 
    } 
    

    而且,你method declarationthrows子句中添加此例外。

    0

    使用

    public void salesTarget() throws OverTimeException { 
        if(salesTargetNum >= unitSold){ 
         throw new OverTimeException(salesTargetNum); 
        } else { 
         System.out.println("Clerk has meet sales target"); 
        } 
    } 
    

    而且您的异常应该声明如下。

    public class OverTimeException extends Exception { 
    
        public OvertimeException(int s){ 
         super("exception caught because sales target is " + s); 
        } 
    
    } 
    
    2

    我强烈建议你重写所有构造函数中Exception类。

    import java.lang.Exception; 
    import java.lang.String; 
    import java.lang.Throwable; 
    
    public class OvertimeException extends Exception { 
        /** 
        * Constructs a new exception with <code>null</code> as its detail message. 
        * The cause is not initialized, and may subsequently be initialized by a 
        * call to {@link #initCause}. 
        */ 
        public OvertimeException() { 
         super(); //To change body of overridden methods use File | Settings | File Templates. 
        } 
    
        /** 
        * Constructs a new exception with the specified cause and a detail 
        * message of <tt>(cause==null ? null : cause.toString())</tt> (which 
        * typically contains the class and detail message of <tt>cause</tt>). 
        * This constructor is useful for exceptions that are little more than 
        * wrappers for other throwables (for example, {@link 
        * java.security.PrivilegedActionException}). 
        * @param cause the cause (which is saved for later retrieval by the 
        *    {@link #getCause()} method). (A <tt>null</tt> value is 
        *    permitted, and indicates that the cause is nonexistent or 
        *    unknown.) 
        * @since 1.4 
        */ 
        public OvertimeException(Throwable cause) { 
         super(cause); //To change body of overridden methods use File | Settings | File Templates. 
        } 
    
        /** 
        * Constructs a new exception with the specified detail message. The 
        * cause is not initialized, and may subsequently be initialized by 
        * a call to {@link #initCause}. 
        * @param message the detail message. The detail message is saved for 
        *    later retrieval by the {@link #getMessage()} method. 
        */ 
        public OvertimeException(String message) { 
         super(message); //To change body of overridden methods use File | Settings | File Templates. 
        } 
    
        /** 
        * Constructs a new exception with the specified detail message and 
        * cause. <p>Note that the detail message associated with 
        * <code>cause</code> is <i>not</i> automatically incorporated in 
        * this exception's detail message. 
        * @param message the detail message (which is saved for later retrieval 
        *    by the {@link #getMessage()} method). 
        * @param cause the cause (which is saved for later retrieval by the 
        *    {@link #getCause()} method). (A <tt>null</tt> value is 
        *    permitted, and indicates that the cause is nonexistent or 
        *    unknown.) 
        * @since 1.4 
        */ 
        public OvertimeException(String message, Throwable cause) { 
         super(message, cause); //To change body of overridden methods use File | Settings | File Templates. 
        } 
    }