Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 1.6 : Learn how to handle exceptions

Tags:

java

exception

I did extensive research on exceptions, but I'm still lost. I'd like to know what is good to do or not.

And I'd also like you to give me your expert opinion on the following example :

public void myprocess(...) {
    boolean error  = false;

    try {

        // Step 1
        try {
            startProcess();
        } catch (IOException e) {
            log.error("step1", e);
            throw new MyProcessException("Step1", e);
        }

        // Step 2
        try {
            ...
        } catch (IOException e) {
            log.error("step2", e);
            throw new MyProcessException("Step2", e);
        } catch (DataAccessException e) {
            log.error("step2", e);
            throw new MyProcessException("Step2", e);       
        }

        // Step 3
        try {
            ...
        } catch (IOException e) {
            log.error("step3", e);
            throw new MyProcessException("Step3", e);
        } catch (ClassNotFoundException e) {
            log.error("step3", e);
            throw new MyProcessException("Step3", e);       
        }

        // etc.

    } catch (MyProcessException mpe) {
        error = true;       
    } finally {
        finalizeProcess(error);
        if (!error) {
            log.info("OK");
        } else {
            log.info("NOK");
        }           
    }
}
  1. Is it ok to throw a personnal exception (MyProcessException) in each step in order to manage a global try...catch...finally ?
  2. Is it ok to manage each known exception for each step ?

Thank you for your help.


EDIT 1 :

Is it a good practice like this ? log directly in global catch by getting message, and try...catch(Exception) in upper level....

The purpose is to stop if a step fail, and to finalize the process (error or not).

In Controller

public void callProcess() {
    try {
        myprocess(...);
    } catch (Exception e) {
        log.error("Unknown error", e);
    }   
}

In Service

public void myprocess(...) {
    boolean error  = false;

    try {

        // Step 1
        try {
            startProcess();
            log.info("ok");
        } catch (IOException e) {
            throw new MyProcessException("Step1", e);
        }

        // Step 2
        try {
            ...
        } catch (IOException e) {
            throw new MyProcessException("Step2", e);
        } catch (DataAccessException e) {
            throw new MyProcessException("Step2", e);       
        }

        // Step 3
        try {
            ...
        } catch (IOException e) {
            throw new MyProcessException("Step3", e);
        } catch (ClassNotFoundException e) {
            throw new MyProcessException("Step3", e);       
        }

        // etc.

    } catch (MyProcessException mpe) {
        error = true;       
        log.error(mpe.getMessage(), mpe);           
    } finally {
        finalizeProcess(error);
        if (!error) {
            log.info("OK");
        } else {
            log.info("NOK");
        }           
    }
}

Thank you.


Edit 2 :

Is it a real bad practice to catch (Exception e) in lower level and to throws a personnal exception ?

like image 454
MychaL Avatar asked Nov 17 '25 21:11

MychaL


1 Answers

Doesn't exist a generic rule,it depends on your needs. You can throw a personal exception, and you can manage each known exception.

But pay attention, it is important what you want.

try{
   exec1();
   exec2(); // if exec1 fails,  it is not executed
}catch(){}

try{
   exec1();
}catch(){}

try{
   exec2(); // if exec1 fails,  it is  executed
}catch(){}
like image 74
Gabriele Mariotti Avatar answered Nov 19 '25 11:11

Gabriele Mariotti