Catch Exception, throw a new Excpetion



  • Cleaning up exception handling in this WTF app so that I can understand why some users are having problems here and there. Of course the logs do not say much sometimes because the exceptions are often swallowed. That isn't seen in this snippet, but this exception probably gets swallowed somewhere. 

     

    public void disconnectPool() throws Exception
      {
        ServletContext context = getServletConfig().getServletContext();
        OracleConnectionCacheImpl oci = (OracleConnectionCacheImpl)context.getAttribute("conPool");
        
        try {
          if (oci != null) {
            oci.close();
          }
        } catch (Exception e) {
          throw new Exception(e.getMessage());
        }
      }

     

     

    That's a pretty common one in this app.



  • I really hate it when people do that. I sometimes set up blocks like this, only so I can breakpoint them if I think I'm going to get an exception: (C#)

     

    try

    {

    dostuff();

    }

    catch (System.Exception e)

    {

    string stringy = e.Message; // I'll breakpoint here

    throw;  //guess what this preserves! Hint: EVERYTHING

    }



  • @Benanov said:

    I really hate it when people do that. I sometimes set up blocks like this, only so I can breakpoint them if I think I'm going to get an exception: (C#)

     

    try

    {

    dostuff();

    }

    catch (System.Exception e)

    {

    string stringy = e.Message; // I'll breakpoint here

    throw;  //guess what this preserves! Hint: EVERYTHING

    }

    I think you can get the debugger to break for you whenever an exception is thrown without needing to modify the source at all.  There should be a menu option somewhere for "Exceptions" or "Event filters" or something like that.



  • If your exceptions all inherit from a common base (they do, don't they?) then you can just set your breakpoint in the constructor for that base exception.

     



  • @smxlong said:

    If your exceptions all inherit from a common base (they do, don't they?) then you can just set your breakpoint in the constructor for that base exception.

    Well, if you use the debugger, you can even break on things like "throw 2;".  Since this is TDWTF after all, don't assume the code that Vechni is debugging is necessarily sane... in fact I think we already have reason to believe it isn't!

     

     



  • @DaveK said:

    Well, if you use the debugger, you can even break on things like "throw 2;".  Since this is TDWTF after all, don't assume the code that Vechni is debugging is necessarily sane... in fact I think we already have reason to believe it isn't!

    Breaking on throw can be useless if the code flings exceptions around like monkey...  err, yeah.  Anyway, since this code likes to swallow its exceptions, it's probable that a lot of them are occurring.



  •  Ah no, this code snippet doesn't have to do with debugging tricks from the previous dev. The previous developer didn't understand Java too well, and in regards to exceptions he just kept writing this until he got something that works. Because the transactions are done manually, throwing exceptions upward is important to prevent the commit further down from the caller. However he just ended up taking an SQLException (checked exception) and turning it into an Exception (checked exception). The method throws Exceptions anyway, which is what he really wanted.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.