When to register chained exceptions?

I am a green developer trying to get a handle (har-har) when handling errors in a large multi-layer java application. There are many situations where I believe that eliminating a chain across multiple layers is a good idea; for example, when a failure when calling an external service on the lowest layer causes all the problems in the view:

  • X content requested but user not logged in
    • caused by: List of authorized users - null
      • caused by: User-management webservice responded Bad Request-The parameter foo should be formatted as "xyz"

The most important exception is the one whose stack I really want to learn is the last in the chain; that I made a bad request and I need to fix the foo formatting. But when I let this exception accumulate through layers that are perfectly tied to the exceptions that make sense for each level ... when I eventually catch and register the thing, the default behavior in the log always shows me the details of the outermost exception and maybe , 5 lines stacktrace root cause.

This makes me want to log exceptions when they occur, and let them bubble, but then you end up logging most things twice; when they occur and when they are eventually caught.

What is the best practice here?

+6
source share
2 answers

I would recommend a different approach to managing exceptions. At the top of the application layer itself (for example, the request entry point), a catch try block is created to raise any exception at run time. It is preferable that you have 2 catch blocks: - for specific applications (business) exceptions - for the rest (exception)

As you can see, you need to introduce your own type of exception, which you will expand to create different exceptions for different purposes. For example, you can create a custom exception for each application layer, for each integrator, etc. Use unchecked exceptions as they will all be handled at the top level. When an exception occurs (catch with a low exception level), you should: - Put a description related to the business context (for example, “failed to load account data from the database” - Add a description of the original exception (for example, “Original error: Connection error with the database ") - Exclude the original exception from your exception so as not to lose tracing - Drop and forget. In other words, the top-level catch block is responsible for processing it accordingly (transaction rollback, error message or what something else you might need

+2
source

Great question, I am curious to know the other answers that you will receive.

I try to use the “more is better” approach and write down at every step of the way. Does it make great magazines? Yes, but when you debug a problem in a large Java application, you will be grateful for every line of the log that you have. There are also tools (at least grep , awk , sed trio) to help you filter large files.

Another way is to write this registration code, but disable it (if you are using something like log4j at the TRACE level). Thus, if you encounter a problem, you may not have available logs, but this is a one-line change (to lower the logging threshold), and you will begin to generate a lot of data for debugging.

In combination with the previous technique, most journal libraries (again, I rely on my knowledge of log4j again) allow you to adjust the log levels of various Java packages. This means that you can write all of these catch and rethrow log lines as traces and reject logging on lower-level packets to WARN while you hold the top-level packets in DEBUG or TRACE .

+1
source

All Articles