我该如何处理此自定义异常?

时间:2019-12-27 18:21:20

标签: java exception error-handling

我有这段代码,它取决于findID()中某个函数引发的自定义异常,它会抛出一个NoClientFound异常,无论何时提及的该函数返回null(我的客户不存在) )。

IDE建议我将异常应用于代码,但是在这段代码中,我需要ID为空(唯一ID),因此我“无法捕获该异常”,因为如果我捕获了该异常,该功能将无法按预期执行。

问题:我该如何处理?

出现异常问题的功能

public boolean add(Client c) {
        StringBuilder sb = new StringBuilder();
        boolean added = false;

        try {
            if (findID(c.getID()) == null) {
                try (BufferedWriter bw = new BufferedWriter(
                        new FileWriter(fitxer, true));) {
                    //Add client to file
                    bw.write(sb.append(c.getID()).append(SEPARADOR).
                            append(c.getName()).toString());
                    bw.newLine();//New line
                    bw.flush(); //Push to file
                    added = true;

                } catch (IOException e) {
                    Logger.getLogger(DaoClient.class.getName()).log(Level.SEVERE,
                            null, "Error appeding data to file" + e);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(DaoClient.class.getName()).log(Level.SEVERE, null,
                    "Error appeding data to file" + ex);
        } finally {

        }
        return addded;
    }

异常代码

public class NoClientFound extends Exception {

    private String msg;    

    public NoClientFound() {
        super();
    }

    public NoClientFound(String msg) {
        super(msg);
        this.msg = msg;
    }

    @Override
    public String toString() {
        return msg;
    }

2 个答案:

答案 0 :(得分:1)

您可以捕获该异常并进行相应处理。当您捕获NoClientFound异常时,这意味着findID(c.getID())为null。因此,无需在if块中进行处理,就可以在catch块中进行处理。

public boolean add(Client c) {
    StringBuilder sb = new StringBuilder();
    boolean added = false;

    try {
        // call the function
        findID(c.getID());

    } catch (NoClientFound ex) {

      //handle the NoClientFound exception as you like here

       BufferedWriter bw = new BufferedWriter(
             new FileWriter(fitxer, true));
       //Add client to file
       bw.write(sb.append(c.getID()).append(SEPARADOR).
       append(c.getName()).toString());
       bw.newLine();//New line
       bw.flush(); //Push to file
       added = true;

    }catch (IOException ex) {
        Logger.getLogger(DaoClient.class.getName()).log(Level.SEVERE, null,
                "Error appeding data to file" + ex);
    }finally {

    }
    return addded;
}

答案 1 :(得分:1)

我假设您已经对null进行了findID(...)的检查

if( c == null || findID(c.getID()) == null){
    throw new NoClientFound("Client not found!");
}else{
    //add your file writing operation
}

并且在NoClientFound类中,它也是从RuntimeException而不是Exception扩展过来的。

public class NoClientFound extends RuntimeException {

...
}

呼叫者方法:

public void caller(){
   Client client = new Client();
   client.setId(1);
   ...
       try{
            add(client);
        }catch(NoClientFound ex){
            //client not found then create one for ex...
        }
        catch(Exception ex){
            //somthing else  happend
            log.error(ex.getmessge());
        }
}