How to combine logging with an exception handling chain?

Suppose I have the following code:

void foo() {
    /* ... */
    try {
        bar(param1);
    } catch (MyException e) {
        /* ??? */
    }
}

void bar(Object param1) throws MyException {
    /* ... */
    try {
       baz(param2);
    } catch (MyException e) {
        /* ??? */
    }
}

void baz(Object param2) throws MyException {
    /* ... */
    if (itsAllATerribleMistakeOhNo) {
        /* ??? */
        throw new MyException("oops, error.");
    }
}

I am wondering where and how I should report the error.

  • In the case of the error below in baz (), I know for sure which operation went awry and can register this fact.
  • At the top, I have the most general context (for example, what is the IP address of the connection, during the processing of which we encountered an error.)
  • Along the way, I may have some kind of context that is unknown neither above nor below.

Another complication is that the error below may not be considered an error when you look at it from above (for example, searching for something in the database fails, maybe you weren’t sure), so I could choose logger.WARN()instead logger.ERROR().

, 3 (, ) - , , . 2x2:

  • /
  • / .

?

: / , , .

+5
4

, :

1) , . , , API , , . API , API .

2) . , , (SQLException), , , , .

3) Re-Throwing exceptions: API , , , .

4) . , .

UPDATE: Java

+2

, . , . , .

foo() ( IP-, , ).

/ . catch throwable, , , , , , - .

. , ApplicationException, (String) (, Enum). , , .

, . , , : - ( , -, () ). , .

:

  • , .
  • ApplicationExceptions .
+2

, . . - , , , , .

, , , , catch, - , . , .

+1

, :

  • . , , , , .
  • , . NullPointerException, .
  • , . , . , SQL. .
  • Not waffles. It's tempting to type technical jargon so that it looks like you are breaking into a matrix. This will not help you in a stressful situation, and it certainly will not help anyone else use your code. Simple English words are always preferable.
  • Finally, NEVER EXCLUDE AN EXCEPTION. Always make sure that you handle the exception, and you somehow output the data following the rules outlined above.
+1
source

All Articles