开发者

execution catching priority

开发者 https://www.devze.com 2023-04-01 09:27 出处:网络
I need to understand java exception handling priority. I have a class Controller, having a class NodeManager having a class NameManager.

I need to understand java exception handling priority.

I have a class Controller, having a class NodeManager having a class NameManager. NameManager has a method:

public LriNode getLriNode(...) throws HttpErrorException
{
   if(condition)
     throw new HttpErrorException (status); 
}

the class NodeManager uses the previous method within the following

public void nodeWriter(...) throws HttpErrorException
{
            ...
    try
    {
        //this can throw an HttpErrorExeption
开发者_如何学运维        lriNode = nameManager.getLriNode(resourceName);
        ...
    }       
    catch (HttpErrorException e)
    {
        if(e.getStatus().name().equals(HttpStatus.NOT_FOUND))
        {
          //here can be thrown an HttpErrorExeption too
           do work
        }
        else
        {
            e.printStackTrace();
        }
    }

}

and finally the class Controller uses the previous method in the following

@RequestMapping(method=RequestMethod.PUT, value="document/{name}")
public ModelAndView putDocument(@RequestBody String body, @PathVariable String name, HttpServletResponse response, HttpServletRequest request)
{
    Source source = new StreamSource(new StringReader(body));

    try
    {

        NodeManager.nodeWriter(...);
        return new ModelAndView(XML_VIEW_NAME, "document", document);
    }
    catch (HttpErrorException e)
    {
        response.setStatus(e.getStatus().value());
        e.printStackTrace();
    }
    return null;
}

Well, that's the point: when NameManager throws a HttpErrorException, I expect the try-catch block within the NodeManger's nodeWriter() method to be executed to the exception. What it happens is that the control is returned to the Controller class and its try-catch block is executed. It seems that the throws HttpErrorException statement in the nodeWriter() method has a priority on the inner try-catch block.

Can anyone explain me such exception handling priority system?


A Throwable does not propagate outside of a try/catch block that handles that type of Throwable. Check the line numbers in the stack trace, and this will tell you exactly where the Exception originates. (Incidentally some java IDEs have an "analyze stacktrace" feature that allows you to click directly to the class and line number in question. I've never found much use for this, but the feature exists, so somebody must have found it useful.)


In your code, you mention that your Controller is handling the exception. Well, in the nodeWriter() method call, your catch suggests that a HttpErrorException can be rethrown.

public void nodeWriter(...) throws HttpErrorException
{
            ...
    try
    {
        //this can throw an HttpErrorExeption
        lriNode = nameManager.getLriNode(resourceName);
        ...
    }       
    catch (HttpErrorException e)
    {
        if(e.getStatus().name().equals(HttpStatus.NOT_FOUND))
        {
          //LOOK HERE!!! --> here can be thrown an HttpErrorExeption too
        }
        else
        {
            e.printStackTrace();
        }
    }

}

If this can rethrow an HttpErrorException, then the Controller that called nodeWriter() will then handle that exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号