Exceptional exception



  • Found this in my company's codebase.

     

                   public static void ThrowException (String strId, bool bLog, object[] args)
                   {
                           String strFormat = GetString (strId);
                           String strMsg = String.Format (strFormat, args);
                           if (bLog)
                                   throw new ApplicationException (strMsg);
                           else
                                   throw new UserException (strMsg);
                   }
     

     

     



  • OK.  I'm not saying that this isn't a WTF, but I could certainly see this being a perfectly valid method for something.



  • @tster said:

    OK.  I'm not saying that this isn't a WTF, but I could certainly see this being a perfectly valid method for something.
     

    I agree, there just isn't even close to enough context here to declare this anything other than 'a method'.



  • I'd like some background, and until I get it, I'm going to humor myself that you have a blog exception.

    Yes, I know it's just a bool to decide what flavor your exception is.



  • @ShaggyB said:

    Found this in my company's codebase.

     

                   public static void ThrowException (String strId, bool bLog, object[ args])
                   {
                           String strFormat = GetString (strId);
                           String strMsg = String.Format (strFormat, args);
                           if (bLog)
                                   throw new ApplicationException (strMsg);
                           else
                                   throw new UserException (strMsg);
                   }
     

     

     

     

    Should compile now.



  • @tster said:

    OK.  I'm not saying that this isn't a WTF, but I could certainly see this being a perfectly valid method for something.
    Maybe I don't know the context, but this is definitely the first time I've ever seen a method which its only function is only to throw an exception.

    Makes me wonder exactly what kind of "logic" would require something like this... 



  • @danixdefcon5 said:

    Maybe I don't know the context, but this is definitely the first time I've ever seen a method which its only function is only to throw an exception.

    Makes me wonder exactly what kind of "logic" would require something like this... 

     

    Just because you don't know of a use for it doesn't make it a WTF.

    If you give 100 programmers the same task you will likely get 100 ways to do it. It is just the nature of the business. You can look at any code example sidebar here and find that out.



  • @tster said:

    OK.  I'm not saying that this isn't a WTF, but I could certainly see this being a perfectly valid method for something.
     

    Most usefull I could think of is:

    if(somethingWentWrong){
        MainClass.ThrowException(":'(", false);
    }

    Still, what's wrong with:

    if(somethingWentWrong){
        throw new Exception(":'(");
    }
    

    Only advantage I could think of is that with a method you can implents notification or debugging.



  • Think localization. GetString might be getting a specific string for the current application culture, so that the exception appears localized in the current language.

    The function is a shortcut for throw new User/ApplicationException( String.Format( Errors.GetString( strID ), args ), which seems somewhat reasonable. I could see it in a centralized Error class of some kind.

    The WTF here, if any, appears to be that there's only two exceptions in the application (one of which gets logged, the other does not). This is subject to debate, since lots of people don't like exceptions at all.



  • @Sunstorm said:

    The WTF here, if any, appears to be that there's only two exceptions in the application (one of which gets logged, the other does not). This is subject to debate, since lots of people don't like exceptions at all.
     

    Since we have no concept of the context for this application, I can't say that not logging certain exceptions is a WTF.



  • @Alistair Wall said:

    @ShaggyB said:

    Found this in my company's codebase.

     

                   public static void ThrowException (String strId, bool bLog, object[ args])
                   {
                           String strFormat = GetString (strId);
                           String strMsg = String.Format (strFormat, args);
                           if (bLog)
                                   throw new ApplicationException (strMsg);
                           else
                                   throw new UserException (strMsg);
                   }
     

     

     

     

    Should compile now.

     

    fail.



  • @MasterPlanSoftware said:

    @Sunstorm said:

    The WTF here, if any, appears to be that there's only two exceptions in the application (one of which gets logged, the other does not). This is subject to debate, since lots of people don't like exceptions at all.
     

    Since we have no concept of the context for this application, I can't say that not logging certain exceptions is a WTF.

    I mean as opposed to more than two exceptions, with more specific variants for the various errors.



  • I'm kind of surprised nobody mentioned the hungarian notation yet.  The concept seems alright, the implementation seems flawed.

     



  • @danixdefcon5 said:

    Maybe I don't know the context, but this is definitely the first time I've ever seen a method which its only function is only to throw an exception.

    Makes me wonder exactly what kind of "logic" would require something like this... 

     

    I have written similar-in-spirit code in other languages (not exceptions, but functions that on the surface you'd wonder why they seemed to do nothing but show an error message, or something). Every time I've written one it's been to use as a callback method for some framework that used callbacks for error conditions.

     Barring that, I like the shortcut-for-much-longer code idea.

    Anything else I've seen I find ridiculous (ie, a valid WTF).



  • @tster said:

    @Alistair Wall said:

    @ShaggyB said:

    Found this in my company's codebase.

     

                   public static void ThrowException (String strId, bool bLog, object[ args])
                   {
                           String strFormat = GetString (strId);
                           String strMsg = String.Format (strFormat, args);
                           if (bLog)
                                   throw new ApplicationException (strMsg);
                           else
                                   throw new UserException (strMsg);
                   }
     

     

     

     

    Should compile now.

     

    fail.

     

    Heheh. Nothing funnier than someone trying to be the code police and then their fix is just as wrong.



  • @Alistair Wall said:

    @ShaggyB said:

    Found this in my company's codebase.

     

                   public static void ThrowException (String strId, bool bLog, object[ args])

                   {

                           String strFormat = GetString (strId);

                           String strMsg = String.Format (strFormat, args);

                           if (bLog)

                                   throw new ApplicationException (strMsg);

                           else

                                   throw new UserException (strMsg);

                   }
     

     

     

     

    Should compile now.

    Actually, no, that wont. That's...

    @ShaggyB said:

    Found this in my company's codebase.

     

                   public static void ThrowException (String strId, bool bLog, object[] args)

                   {

                           String strFormat = GetString (strId);

                           String strMsg = String.Format (strFormat, args);

                           if (bLog)

                                   throw new ApplicationException (strMsg);

                           else

                                   throw new UserException (strMsg);

                   }
     

     

     


Log in to reply