Hongli Bu
Hongli Bu

Reputation: 561

How to suppress errors in one specific class in Java

I write a class in Java and Spring Boot. It is a service, and in the service, it calls other libraries. The problem is when I call the libraries, it will log some errors, these erros actually didn't affect the execution of the service.

So I am wondering, can I suppress the errors in the class?

The service class looks like below.

@Service
public class serviceImpl implements service {
    @Override
    public String executeComputation(String rawData, String computationName)
            throws BrokerException, IOException {
    //call some libs
    }
}

The error looks like this:

Unexpected exception during (something) evaluation. Details: Cannot invoke method collectEntries() on null object. Source Code: import java.text.DateFormat;

Upvotes: 0

Views: 2369

Answers (1)

Aniket Sahrawat
Aniket Sahrawat

Reputation: 12937

Write an aspect for it. An example of aspect:

@Aspect
public class MyAspect {

    @Around("thePointcutExpression")
    public Object executeComputationAspect(ProceedingJoinPoint pjp) throws Throwable {
        Object ob;      
        try {
            ob = pjp.proceed();
        } catch(Exception e) {} // swallow the exception
        return ob;
    }

}

Upvotes: 2

Related Questions