Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why spring handles only unchecked exceptions

I want to know why spring handles only unchecked exceptions..... can any one explain what is the reason behind this .

Spring is using any design patterns which will avoid checked exceptions ?

like image 276
Girish Avatar asked Apr 20 '14 05:04

Girish


People also ask

Why are exceptions unchecked in spring?

Because the compiler can't anticipate logical errors that arise only at runtime, it can't check for these types of problems at compile time. That's why they are called "unchecked" exceptions. Unchecked exceptions result from faulty logic that can occur anywhere in a software program.

Why unchecked exceptions are not handled?

An unchecked exception (also known as an runtime exception) in Java is something that has gone wrong with the program and is unrecoverable. Just because this is not a compile time exception, meaning you do not need to handle it, that does not mean you don't need to be concerned about it.

How does Spring controller handle exceptions?

Spring MVC Framework provides following ways to help us achieving robust exception handling. Controller Based - We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation. This annotation takes Exception class as argument.

How does spring boot handle runtime exceptions?

Exception Handler Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown. This method should be used for writing the Controller Advice class file. Now, use the code given below to throw the exception from the API.


2 Answers

Spring is using any design patterns which will avoid checked exceptions ?

Not a design pattern but Best Practices for Exception Handling.

Consider below code:

public void consumeAndForgetAllExceptions(){
    try {
        ...some code that throws exceptions
    } catch (Exception ex){
        ex.printStacktrace();
    }
}

What is wrong with the code above?

Once an exception is thrown, normal program execution is suspended and control is transferred to the catch block. The catch block catches the exception and just suppresses it. Execution of the program continues after the catch block, as if nothing had happened.

How about the following?

public void someMethod() throws Exception{
}

This method is a blank one; it does not have any code in it. How can a blank method throw exceptions? Java does not stop you from doing this.

I want to know why spring handles only unchecked exceptions?

Personally I prefer unchecked exceptions declared in the throws cause. I hate having to catch exceptions when I'm not interested in them. I agree the spec needs to a few more exception types, but I disagree that they should be checked. Most frameworks rely on unchecked exceptions, not only Spring framework.

Best Practices for Designing the API

  • If the client can take some alternate action to recover from the exception, make it a checked exception.
  • If the client cannot do anything useful, then make the exception unchecked. By useful, I mean taking steps to recover from the exception and not just logging the exception.

The Java API has many unchecked exceptions, such as NullPointerException, IllegalArgumentException, and IllegalStateException. I prefer working with standard exceptions provided in Java rather than creating my own. They make my code easy to understand and avoid increasing the memory footprint of code.

See Also:

  • Unchecked Exceptions
  • Best practices in exception handling
like image 149
Abhishek Nayak Avatar answered Oct 05 '22 20:10

Abhishek Nayak


Spring's central theme is Dependency Injection and IOC. Checked exceptions add unncessary code and create more dependency. As such, the dependency costs of checked exceptions outweigh their benefits. They reduce the ability to test code. They lead to lower cohesion and higher coupling. The goal of your code should be high cohesion and low coupling. Spring is focused towards the S.O.L.I.D principles of design.

like image 45
Sean Gildea Avatar answered Oct 05 '22 20:10

Sean Gildea