Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何处理此自定义异常?_Java_Exception_Error Handling - Fatal编程技术网

Java 如何处理此自定义异常?

Java 如何处理此自定义异常?,java,exception,error-handling,Java,Exception,Error Handling,我有一段代码,它依赖于findID()中的一个函数抛出的自定义异常,每当提到的函数返回null(客户端不存在)时,它抛出一个NoClientFound异常 IDE建议我将该异常应用到代码中,但在这段代码中,我需要ID为null(唯一ID),我“无法捕获该异常”,因为如果捕获它,函数将不会按预期执行 问题:我如何处理这个问题? 有异常问题的函数 public boolean add(Client c) { StringBuilder sb = new StringBuilder()

我有一段代码,它依赖于
findID()
中的一个函数抛出的自定义异常,每当提到的函数返回null(客户端不存在)时,它抛出一个
NoClientFound
异常

IDE建议我将该异常应用到代码中,但在这段代码中,我需要ID为null(唯一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;
    }

您可以捕获该异常并相应地处理它。当捕捉到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;
}

我假设您已经在
findID(…)

并且在
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());
        }
}

因此,
c.getID()
可以抛出异常,但您希望
findID()
被调用,即使
c.getID()
抛出?@johnnymapp我需要它,因为我需要它来保持ID的唯一性。整个代码不是我的,我的全部任务是添加异常。我可以将布尔值“added”添加到finally,它看起来也会正确,对吗?在这种特殊情况下,它必须返回一个布尔值。我正在测试你的答案,如果它有效,我会接受。我刚刚看到你在顶部添加了false。因此,您可以在finally块中返回added。您能简要解释一下为什么是运行时而不是一般异常吗?我最初的目的是获取任何错误,所以我使用了扩展异常。这只是一个快速修复,当调用
add(…)
get而找不到客户端时,它会在运行时引发异常,并且您的IDE在编译代码时不会抱怨。但是最好的方法是在调用者中处理异常,比如如果找不到客户端,然后执行不同的操作,我也添加调用者方法。我希望这会有帮助。
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());
        }
}