Java force catch RuntimeException?

Can java get you to catch RuntimeExceptions? In particular, I work with the Spring framework, and the entire Exception hierarchy is based on RuntimeExceptions. Many times I forget to try and catch Exceptions. A specific example is an LDAP query or SQL call.

+4
source share
9 answers

Not just with Java.

But if you compile using the AspectJ compiler or modify your classes using weaving AspectJ load time, then this is possible.

Definition of an aspect from Wikipedia :

aspect is part of a program that solves major problems

Aspects are often used to add, for example, transaction logic and security.

To demonstrate how an aspect can solve your problem, I created an example that catches all exceptions returned from a call to the integration level from within a method in classes marked with @Service annotation. In this case, classes that contain .integration. in the name of the package.

This is just an example. You can modify it to intercept RuntimeExceptions in other places. For example, in all methods inside a class that has Facade in its name and calls other methods. See the picture below for ideas:

alt text
(source: espenberntsen.net )

The orange arrows are my AspectJ pointpect, illustrated with the AJDT plugin in Eclipse. These are places where you can catch and AfterThrowing exceptions in the AfterThrowing board.

Here is a tip:

 @AfterThrowing(value="serviceMethodAfterExpcetionFromIntegrationLayerPointcut()", throwing="e") public void serviceMethodAfterExceptionFromIntegrationLayer(JoinPoint joinPoint, RuntimeException e) { StringBuilder arguments = generateArgumentsString(joinPoint.getArgs()); logger.error("Error in service " + joinPoint.getSignature() + " with the arguments: " + arguments, e); } 

My serviceMethodAfterExpcetionFromIntegrationLayerPointcut actuall consists of two other poincuts:

  @Pointcut("call(* *..integration.*.*(..))") public void integrationLayerPointcut() {} @Pointcut("within(@org.springframework.stereotype.Service *)") public void serviceBean() {} @Pointcut("serviceBean() && integrationLayerPointcut()") public void serviceMethodAfterExpcetionFromIntegrationLayerPointcut() {} 

@Service , pointcut finds all places in the classes marked with the @Service annotation that call the method in the class at the integration level. (See the orange arrows in the picture)

If you want to catch runtime exceptions in your test classes, you can change the pointcut to this:

 @Pointcut("within(com.redpill.linpro.demo..*Test)") public void inDemoProjectTestClass() {} 

Then the above tip will catch all exceptions in test classes that end in Test in the package ..demo.

More information about @AspectJ 's style here .

AspectJ integrates well with Spring. General information about AspectJ can be found here, and Spring AspectJ support here .

+7
source

No: RuntimeException , and its subclasses are marked exceptions.

JLS 11.2.5 Why Runtime exceptions are not checked

Runtime exception classes ( RuntimeException and its subclasses) are exempted from compilation time checking, because, according to the developers of the Java programming language, the need to declare such exceptions would not significantly help to establish the correctness of the program. Many of the operations and constructs of the Java programming language can throw runtime exceptions. The information available to the compiler and the level of analysis that the compiler performs are usually insufficient to establish that such exceptions at run time cannot occur, although this may be obvious to the programmer. The requirement that such exception classes be declared would simply annoy programmers.

JLS 11.5 Exception Hierarchy

The Exception class is the superclass of all exceptions that regular programs might want to recover. The RuntimeException class is a subclass of the Exception class. RuntimeException subclasses are unchecked exception classes. Exception subclasses other than RuntimeException , and its subclasses, are all tested exception classes.

+5
source

Not. Subclasses of RuntimeException are never checked.

+2
source

In fact, Spring exceptions are thrown at runtime for a good reason. Spring will throw checked exceptions at runtime because if, for example, a SQLException is thrown because the database is down, you are unlikely to be able to repair or do anything meaningful.

Also, with checked exceptions, your code becomes more confusing and clumsy, especially if you need to have another try / catch block in the catch block.

+1
source

No, you cannot force the compiler to force you to catch any RuntimeException .

Mostly because the method you are calling does not declare that it is throwing a RuntimeException, the compiler cannot know about any exceptions that can be thrown, or make you catch them.

0
source

No, It is Immpossible.

What you can do, but poor design and definitely not recommended , is that you throw throw Exception

 public void doStuff() throws Exception {} 

Thus, forcing any caller to catch an Exception . This includes RuntimeException s, but this is a poor design since the scope is very wide and does not indicate which exception is thrown.

0
source

Not. The only thing I can think of is to use a tool like FindBugs, PMD, etc., to find these problems for you, although I do not believe that any tool has a tool specific to this. However, you can write your own detector. Of course, this only applies to explicit throws of RuntimeExceptions, and not to implicit ones like NullPointerException.

0
source

Instead of forcing you to remember all the fun exceptions around the world, I would recommend writing a wrapper for some spring calls.

So, instead of being forced to remember

  try { SpringThing.doThatOneMethod(); catch (SpringConnectionException sce) { /*handle it*/ } catch (SpringProxyException spe) { /*handle it*/ } catch (SpringRuntimeException sre) { /*handle it*/ } 

you can always call your own SpringExceptionsWrapper.doThatOneMethod() .

If you really wanted to, you could have method signatures for your wrapper functions, a list of some checked exceptions that you could wrap stuff in. I would not encourage it, as it makes you handle it everywhere.

0
source

RuntimeExceptions can be caught and processed, such as Checked Exceptions, but they do not need to be processed. For example, if you select a new NullPointerException () in a try block, the compiler will not force you to put the throw keyword in your method declaration, as if throwing a Checked exception. Handling a RuntimeException or its subclass indicates a bad programming style and should be avoided.

0
source

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


All Articles