Always try to catch - finally, with the exception? Central error management?

I wonder if I always have to use try-catch-error blocks that clutter up the code if I want to catch an error.

Or can one somehow define a global error trap? Especially regarding Java EE Webapps.

For each raw ex, I would like to register in a particular file and display a common error page for the user.

I thought I could achieve this through aspects. But in order to catch @AfterThrowing , I also have to introduce try-catch blocks. And since there is no central class for facade facades, I would have to surround each support method with trycatches. Then the aspect will take them, but I need to catch something without obvious throw exceptions.

How can i do this?

+4
source share
4 answers

You are looking for the declare soft construct. This will wrap this exception in SoftException (AspectJ-specific RuntimeException) so that it does not need to be explicitly called. You can then handle all of these exceptions with the AfterThrowing .

declare soft exists only in AspectJ code style (i.e. there is no annotation for this). So, you will need to compile your code using the AspectJ compiler, but you can use the load time in time for this.

See here: http://www.eclipse.org/aspectj/doc/released/progguide/quick-other.html

And here: http://www.eclipse.org/aspectj/doc/released/adk15notebook/declare-soft.html


EDIT

Here is a snippet of code that shows how to do this:

 aspect ErrorHandler { declare soft : Exception : within(*); after() throwing(Exception e) : handler(e) { // do something... } } 

This will redirect all exceptions from your system using a special error handler. And you donโ€™t need to explicitly catch or throw them away.

It is simple and powerful. Perhaps too powerful. I would recommend clarifying and clarifying which exceptions should be mitigated and which ones should be advised, but this is the main idea.

+3
source

You do not need to do this in every method.

You should not catch an exception that cannot be handled. Processing means more than just reorganizing or logging or printing a stack trace. I believe that processing means implementing a meaningful recovery strategy.

This may mean that the โ€œdollar stops hereโ€: you are Gandalf on the bridge at the edge of the layer boundary, and no exception will fail. You do not want users to see unpleasant messages, so you catch them and send them to a familiar, easily understood page that tells them what to do next.

Lastly, this is not always necessary, but it is ideal for cleaning up resources such as file descriptors and database cursors.

If you cannot handle the exception, there is no shame in adding a throws clause to the method signature and letting callers figure out what they want to do.

+1
source

In general, there is no mechanism for this - Java does not have what you are looking for.

However, depending on your circumstances, this may be possible.

web.xml exception handler

The web.xml allows you to specify the URL that will be used to handle the specified type of exception. (See, for example, http://wiki.metawerx.net/wiki/Web.xml.ExceptionType ).
Since you are writing webapp, you can simply let the exceptions drop all the way to the top and then handle them that way.

Custom interceptor

You mentioned that you have backup facades. Depending on how they are created, you can put a common proxy in front of them to catch and handle those exceptions that are of interest to you. You tagged your question with spring so you might want to check out Spring AOP proxies.

There may be other ways to get what you want, but it will depend on the specifics of your application architecture.

+1
source

The finer the control has exceptions, the easier it will be to debug / provide a meaningful message.

In what degree? I would relate this to the complexity / expected life of your application. The larger it is, the better your appeal should be.

I see two main approaches:

  • User Approach: You get at least one exception handling for each UI action (so you can say: "Do not click this button AGAIN).

  • Debugger: each method has its own control.

Keep in mind that most of the processing can simply be logging the reorganization of the exception.

Moreover, most likely, your Java EE infrastructure will have log parameters in its configuration files (many of them work with java.util.loggin or log4j). You could customize this; Of course, what is sent to each category of the journal will depend on the implementation of the structure (therefore, perhaps not all ERROR messages will be Exceptions).

0
source

Source: https://habr.com/ru/post/1400306/


All Articles