Results 1 to 10 of 10
  1. #1
    andy16 is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default Exception throwing

    Hey, I have a small problem with throwing an exception. I am using a custom exception. I will leave out the most basic stuff and get right to the problem. Basically I have a GUILayer, ControlLayer and DbLayer. When I throw my exception in my DB class, it is caught in the very same method (because it is thrown within a try clause). What I want, however, is to catch it in my GUILayer instead.


    GUI class:
    Java Code:
    try {
        CtrPerson ctrP = new CtrPerson();
        Person person = ctrP.findPerson(cpr);
    }
    catch (NotFoundException nFE) {
        // I want to catch the exception here rather than in the DB class
    }

    Controller class:
    Java Code:
    // Initialization of dbP omitted
    public Person findPerson(String cpr) throws NotFoundException {
        return dbP.findPerson(cpr);
    }

    DB class:
    Java Code:
    public Person findPerson(String cpr) throws NotFoundException {
        try {
            // MSSQL query omitted
    
            if (results.next()) {
                 // Patient found
            }
    
            else throw new NotFoundException("Person not found.");
        }
    
        catch (Exception e) {
            // NotFoundException is caught here!
        }
    So basically I want to catch the NotFoundException that I throw in my DB class in my GUI class instead. Is there a way to do this other than throwing it within the Exception catch?

    Thanks in advance.

  2. #2
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Hi! I am not certain, but it seems the reason your custom exception is caught in your DbLayer is because every exception is a sub class of Exception. Hence every exception is an Exeption :D

    Maybe you should try to catch something else than an Exception object. Make sure that the object you catch is not a super class for for your custom exception.

    Edit: Now that I think about it, last sentence did not make any sense :D Hence the whole premise falters. Ignore this post and wait for someone else to answer!

    Edit: Hey! I was right :D I tried your code and caught another Exception object than Exception and it threw the object to the correct catch clause!
    Last edited by BruteforceFtw; 06-07-2010 at 01:11 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Rethrow that exception after you have cought it:

    Java Code:
    }
    catch (NotFoundException nfe) {
       throw nfe; // throw it again
    }
    catch (Exception e) {
       // handle all the other exceptions
    }
    kind regards,

    Jos

  4. #4
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    @JosAH

    Hey! It seems awkward doesn't it ? If you for instance had used the catch clause in this manner :

    Java Code:
    catch (Exception e) {
       // handle all the other exceptions
    }
    catch (NotFoundException nfe) {
       throw nfe; // throw it again
    }
    nfe would not be caught and thrown again. It seems that its better to simply not to use Exception in the first place to catch the object, but instead use some subclass of Exception instead.

    With kind regards

    Edit : I tried to compile the code, but some how I got an exception : exception NotFoundException has already been caught. Anyone mind explaining that ?
    Last edited by BruteforceFtw; 06-07-2010 at 01:27 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by BruteforceFtw View Post
    @JosAH

    Hey! It seems awkward doesn't it ? If you for instance had used the catch clause in this manner :

    Java Code:
    catch (Exception e) {
       // handle all the other exceptions
    }
    catch (NotFoundException nfe) {
       throw nfe; // throw it again
    }
    nfe would not be caught and thrown again. It seems that its better to simply not to use Exception in the first place to catch the object, but instead use some subclass of Exception instead.

    With kind regards

    Edit : I tried to compile the code, but some how I got an exception : exception NotFoundException has already been caught. Anyone mind explaining that ?
    Exceptions in catch clauses are checked from top to bottom; the first exception that is a superclass or a class equal to the thrown exception is executed. Therefore if you mention Exception first it'd catch all types of exceptions (Exception is the mother of all exceptions). The compiler checks that.

    kind regards,

    Jos

  6. #6
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    @JosAH

    Ah, that makes sense. I actually suggested that previously and this verifies it. But doesn't this show that Andy16 should simply change what type of object he wants to catch in his DbLayer class ? Exception is simply to crude to use in the first place(Sorry Andy :D). Perhaps use some subclass of Exception which might be sensible to use.

    With Kinds Regards,
    BruteForceFTW

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Java Code:
        try {
            // MSSQL query omitted
    
            if (results.next()) {
                 // Patient found
            }
    
            else throw new NotFoundException("Person not found.");
        }
    
        catch (Exception e) {
            // NotFoundException is caught here!
        }
    Are you catching Exception to avoid catching SQLException?
    That's poor practice to start with (as you've seen).
    Just catch SQLException and that will solve your problem.

  8. #8
    andy16 is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by BruteforceFtw View Post
    Hi! I am not certain, but it seems the reason your custom exception is caught in your DbLayer is because every exception is a sub class of Exception. Hence every exception is an Exeption :D

    Maybe you should try to catch something else than an Exception object. Make sure that the object you catch is not a super class for for your custom exception.
    Quote Originally Posted by BruteforceFtw View Post
    It seems that its better to simply not to use Exception in the first place to catch the object, but instead use some subclass of Exception instead.
    Quote Originally Posted by BruteforceFtw View Post
    Ah, that makes sense. I actually suggested that previously and this verifies it. But doesn't this show that Andy16 should simply change what type of object he wants to catch in his DbLayer class ? Exception is simply to crude to use in the first place(Sorry Andy :D). Perhaps use some subclass of Exception which might be sensible to use.
    Quote Originally Posted by Tolls View Post
    Are you catching Exception to avoid catching SQLException?
    That's poor practice to start with (as you've seen).
    Just catch SQLException and that will solve your problem.

    Hello again and thank you for the replies everyone.

    The reason why I catch Exception and not something more specific is if something else should go wrong (something that has nothing to do with SQLException for instance). I thought of it as a way to ensure that the end user does not experience a crash. I often catch more specific exceptions above if I can see that they can occur, but still catch Exception eventually to be safe. Say that I process the SQL query results within that method, then several other errors could occur (some of which can be hard to predict I imagine (I have not dealt with a whole lot of specific exceptions to be honest)). Surely a way to counter that would be to process the results in a separate method, but that may not always be desirable, especially if the processing is fairly simple. Whether I am talking nonsense, I don't know, but at least this is why I chose to do it the way I did. ;)

    JosAH's approach would work, but is it the best way to go about it? I cannot come up with a better one, but there is a reason why I am still glued to the school desk. :)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    You catch Exception near to your presentation layer to, as you say, shield the user from ugly errors.

    This is not one of those methods, though. At least it shouldn't be.

    Catch the SQLException, and then test the method properly.

  10. #10
    andy16 is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 11
    Last Post: 11-05-2009, 12:44 PM
  2. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 02:34 PM
  3. throwing bug value
    By Dangi in forum New To Java
    Replies: 3
    Last Post: 10-25-2008, 06:14 AM
  4. Display Message Without Throwing Exception
    By kailashchandra in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-27-2008, 09:05 AM
  5. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •