how to apply #ControllerAdvice in intellij idea? - spring

i am working with intellij idea.
and i plan to make use of #ControllerAdvice to handle exceptions.
then intellij reports "unhandled exception", because i don't have a try-catch block in my code. then how to solve it? there must be a way.

If you're not going to handle the checked exceptions with a try catch, you'll need to rethrow the exception in the method signature. This will force the method that calls that method to handle the exception
public void someMethod() throws IOException {
}
https://docs.oracle.com/javase/tutorial/essential/exceptions/

Annotations can not affect language semantics. You may use #ControllerAdvice to handle exceptions at runtime, but they must be declared for the code to be accepted by the Java compiler.

Related

Exception handling with Kotlin with Spring

Roman Elizov has a great blog post on how to use exceptions with Kotlin. He emphasizes that catching exceptions in kotlin is usually code smell.
But does that mean that I should be able to throw exceptions freely in my Spring application if I am using application level exception handlers?
More specifically let's say I have function that looks up an item in the database, and a controller that calls this service. If the item is not in the database, should I return a nullable from the service or should I throw an exception? (The controller doesn't have to try/catch the exception because all applications are handled at framework level)
It depends on your use case and context. But generally I would say that you should throw an exception annotated with #ResponseStatus(HttpStatus.NOT_FOUND).

Difference between try catch and #ExceptionHandler in Spring REST

I was going through a video tutorial where the instructor focussed on handling any Runtime exceptions through the #ExceptionHandler in Spring.
What is the difference between the approach of handling it via try catch or via #ExceptionHandler?Which is the better approach and when to use either of them?
bro we use #ExceptionHandler above a method which we want to handle all the exception that occurs in your code while in case of try and catch you just handle the exception where it occurs only. But if we want that every exception that occurs in our program to be handled by one method, so that it will look clean and professional, we use #ExceptionHandler. Here is the link that might help you understand this.
https://howtodoinjava.com/spring-core/spring-exceptionhandler-annotation/
Let me know if this helps.

How to get a thrown exception from MailerClient of Playframework

I'm working with Play-Mailer inside of a Play application. I'm able to send emails to correct email addresses. So far so good. - But with none existing email addresses an exception is thrown from org.apache.commons.mailer but method mailerClient.send(email) itself throws nothing. - Is there a way to catch any exceptions form Play-Mailer?
EDIT:
I looked at the Play-Mailer plugin and saw that it is written in Scala. Scala does not have the notion of checked exceptions. Scala compiles directly to JVM bytecode and this is the "trick" how you can call Java code in Scala which throws checked exceptions but you do not have to rethrow them (the rules for checked exceptions are a bit different on JVM bytecode level).
You are free to use standard try-catch block in your Java code to handle the exceptions.
What kind of exceptions is the commons mailer throwing? Are you referring to: https://commons.apache.org/proper/commons-email/javadocs/api-release/org/apache/commons/mail/Email.html#send--
The send() methods throws an EmailException which is not a RuntimeException, so the exception would be propagated. If the Play-Mailer plugin is not catching it, you have to do it in your code:
try {
// try to send the mail
mailer.send(email);
} catch (EmailException e) {
// do something with the exception
}

What is the replacement for Spring's deprecated NoSuchRequestHandlingMethodException?

The NoSuchRequestHandlingMethodException was deprecated in Spring 4.3, in favor of annotation-driven handler methods. What does this mean? The exception is still listed in the documentation, without mentioning its deprecated status. If I understand correctly, this exception is thrown when there is no request mapper for a given request. It appears to be handled by the DefaultExceptionHandlerResolver, here, and the relevant method has been deprecated as well.
If this method is deprecated, can I assume Spring does not throw this exception anymore? How am I supposed to replace this functionality with annotation-driven exception handling? Which exception am I supposed to handle, if this one is deprecated?
Side note: I also noticed a newer NoHandlerFoundException, here. Is this a replacement? If so, why? It appears to do the same thing. And why are the exceptions related to other HTTP status codes not deprecated? It all doesn't make a lot of sense.
The NoSuchRequestHandlingMethodException exception is part of the multiaction package that has been deprecated in Spring:
https://docs.spring.io/spring-framework/docs/2.5.x/javadoc-api/org/springframework/web/servlet/mvc/multiaction/class-use/NoSuchRequestHandlingMethodException.html
If you don't use multiactions, you can safely get rid of that catch statement and/or stop trying to catch and handle that exception. For example, some "Exception-to-Response error handlers" sample code might look like this to try and catch cases when the dispatcher doesn't find a proper mapping:
#ControllerAdvice
public class RestErrorHandler {
#ExceptionHandler({FileNotFoundException.class, NoSuchRequestHandlingMethodException.class})
#ResponseStatus(HttpStatus.NOT_FOUND)
#ResponseBody
public ErrorInfo process404(HttpServletRequest req, Exception ex) {
return handleException(req, HttpStatus.NOT_FOUND, ex);
}
}
But the latest dispatcher (non-multiaction) will never throw such an exception, so you could simply get rid of the NoSuchRequestHandlingMethodException and, instead, handle the NoHandlerFoundException (which is not thrown by default, but you can configure the spring dispatcher to throw it IF you really need to, because, by default, the dispatcher already returns a 404).

Error Handling when using annotations

I'm following the documentation (State Machine Error Handling) to implement error handling. However, when an exception occurs it is propagated up rather than intercepted. I tried using the interceptor, the listener and the #OnStateMachineError without any success. Debugging the code, neither MethodInvokingStateMachineRuntimeProcessor.java:52 or any of its callers have any specific logic to handle errors.
Replicating the issue is simple, just create a state machine (I'm using the latest snapshot) and register the bean:
#WithStateMachine
public class ExceptionThrowingAction {
#OnTransition
public void throwError(#EventHeaders Map<String, Object> headers, ExtendedState extendedState) {
throw new RuntimeException("test error");
}
}
Am I missing something or is it a genuine bug? If so, I'll raise as an issue
Yes, this is a bug. We've done a lot of changes in master to harden there user level hooks. None of those should break machine execution. Please raise an issue and we'll fix it.

Resources