Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java force catch RuntimeException?

Is it possible to force java to make you catch RuntimeExceptions? Specifically I am working with the Spring framework and the whole Exception hierarchy is based upon RuntimeExceptions. A lot of the times I forget to try and catch the Exceptions. A specific example is when doing an LDAP query, or an SQL call.

like image 635
wuntee Avatar asked Dec 07 '22 03:12

wuntee


2 Answers

Not with just Java.

But if you compile with the AspectJ compiler or modifies your classes with AspectJ load-time weaving, then it's possible.

A definition of an aspect from Wikipedia:

an aspect is a part of a program that cross-cuts its core concerns

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

To demonstrate how an aspect can solve your problem, I have created an example that catches all exceptions thrown back from call to the integration layer from inside a method in classes marked with the @Service annotation. In this case classes that contains .integration. in the package name.

This is just an example. You can modify it to catch RuntimeExceptions other places. For example in all methods that's 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 pointcut illustrated with the AJDT plugin in Eclipse. This is the places where you can catch and deal with the runtime exceptions in an AfterThrowing advice.

Here is the advice:

@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 consist 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() {}

Short summarized, the pointcut finds all places in classes marked with the @Service annotation that call on a method in a class on the integration layer. (See orange arrows on the picture)

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

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

Then the advice above will catch all exceptions in the test classes that ends with Test in the ..demo package.

More information about @AspectJ style here.

AspectJ is well integrated with Spring. General information about AspectJ can be found here and Spring's AspectJ support here.

like image 82
Espen Avatar answered Dec 15 '22 08:12

Espen


No: RuntimeException and its subclasses are unchecked exceptions.

JLS 11.2.5 Why Runtime Exceptions are not Checked

The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs. Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer. Requiring such exception classes to be declared would simply be an irritation to programmers.

JLS 11.5 The Exception Hierarchy

The class Exception is the superclass of all the exceptions that ordinary programs may wish to recover from. The class RuntimeException is a subclass of class Exception. The subclasses of RuntimeException are unchecked exception classes. The subclasses of Exception other than RuntimeException and its subclasses are all checked exception classes.

like image 25
polygenelubricants Avatar answered Dec 15 '22 06:12

polygenelubricants