Why doesn't the error fall even when it is clearly thrown?

I want to catch the "Error" in SpringMVC3 using the annotated "@ExceptionHandler". I can catch the throw and any exception, but when I tried with "Error", it did not catch the exception. Any idea why? The code below demonstrates the problem.

@Controller
@RequestMapping("/test")
public class TestExceptionController {

static final Logger logger = Logger.getLogger(TestExceptionController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String processData(int intValue) throws InvalidDataException {

        if (intValue < 6) {
            try {
                throw new Exception();
            } catch (Exception e) {
                throw new InvalidDataException();
            }
        }

        return "test";

    }

    @ExceptionHandler(InvalidDataException.class)
    public ModelMap handleException(InvalidDataException ex) {
        logger.debug("exception catched  :" + ex);

        return new ModelMap();

    }
}

The above code is caught, but the code is not caught below. Why does this not catch the error?

@Controller
@RequestMapping("/test")
public class TestExceptionController {

    static final Logger logger = Logger.getLogger(TestExceptionController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String processData(int intValue) throws Error{

        if (intValue < 6) {
            try {
                throw new Exception();
            } catch (Exception e) {

                throw new Error();
            }
        }
        return "test";

    }

    @ExceptionHandler(Error.class)
    public ModelMap handleException(Error ex) {
        logger.debug("exception catched  :" + ex);

        return new ModelMap();

    }
}
+3
source share
5 answers

In fact, I looked at the source of the spring DispatcherServlet and on line 809 it explains why the Error could not be processed

        catch (Exception ex) {
            Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
            mv = processHandlerException(processedRequest, response, handler, ex);
            errorView = (mv != null);
        }

- , spring ExceptionResolvers, , bean. , spring cathces Exception not Throwable. Exception, , spring . @ExceptionHandler, , .

+4

Error javadocs:

- Throwable, . . ThreadDeath, "" , Error, .

( Throwable, Exception) - . Spring , .

+1

, , @peter-szanto, , java.lang.Error Spring - . , . web.xml / 500, / a Spring - ( JSP), . , . , Spring URI, Spring.

0

Spring 3.0.5 MVC

@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException{
    public NotFoundException() {
        super();
    }
}

 @ExceptionHandler
    @RequestMapping(method = RequestMethod.GET, value = "employee/{id}",
            headers = "Accept=application/json,application/x-protobuf,application/xml")
    public @ResponseBody method(xxxx)
{
..
throw new NotFoundException
...
}
-1

not meant to handle Exception instead of Error? The error is very correct in Java and has very few implementing classes.

-2
source

All Articles