Correct error handling?



  • I wonder if this kind of error handling is WTFish or if it is a good way to  do it.

        public SomeClass(HashMap<String, String> configMap) throws CriticalPreferenceMissingException {
            if(configMap.isEmpty()) {
                throw new IllegalArgumentException("Configuration HashMap can't be emty.");
            }
           
                string1 = configMap.get("string1");
                string2 = configMap.get("string2");
                string3 = configMap.get("webappRoot");
                integer1 = Integer.parseInt(configMap.get("integer1"));
                boolean1 = Boolean.parseBoolean(configMap.get("boolean1"));
               
                if(string1 == null || string2 == null || string3 == null) {
                    int errorCode =
                        string1 == null ? 1 : 0 +
                        string2 == null ? 2 : 0 +
                        string3 == null ? 4 : 0;
                   
                    switch (errorCode) {
                        case 1:
                            throw new CriticalPreferenceMissingException("string1 not set.");
                        case 2:
                            throw new CriticalPreferenceMissingException("string2 not set.");
                        case 3:
                            throw new CriticalPreferenceMissingException("string1 and string2 not set.");
                        case 4:
                            throw new CriticalPreferenceMissingException("string3 not set");
                        case 5:
                            throw new CriticalPreferenceMissingException("string1 andstring3 not set");
                        case 6:
                            throw new CriticalPreferenceMissingException("string3 and string2 not set");
                        case 7:
                            throw new CriticalPreferenceMissingException("string1, string2 and string3 not set");
                    }
                }
        }



  • @Laertes said:

    I wonder if this kind of error handling is WTFish or if it is a good way to  do it.

    IMO it's a small WTF. Let's assume that in the next version, we have 7 more critical preferences.

    Can you imagine

                    switch (errorCode) {
                        case 1:
                            throw new CriticalPreferenceMissingException("string1 not set.");
    ...

                        case 517:
                            throw new CriticalPreferenceMissingException("string1, string3 and string10 not set.");

    ... 

                        case 1023:
                            throw new CriticalPreferenceMissingException("string1, string2, string3, string 4, string5, string6, string7, string8, string9 and string10 not set.");

                    }
                }

     

    I'd prefer a StringBuilder:

    if(string1 == null || string2 == null || string3 == null) {

        StringBuilder sb = new StringBuilder();

        sb.append("missing settings:");

        if (string1==null) sb.append(" string1 ");

        if (string2==null) sb.append(" string2 ");

        if (string3==null) sb.append(" string3 ");

        throw new CriticalPreferenceMissingException(sb.toString());
    }

     



  • You got a valid point threre.

    But your solution will also grow with the amount of preferences; although linear not exponential as in my case ;)

    I wonder if there is a more generic way to do this, a solution that doesnt grow at all.
    Like getting all critical preferences in a set, looping through all of them and build the string.
    But to keep it generic, i'd need to get to the names of the variables somehow...

    Well listen to me thinking aloud ;)
    Nevermind, i'll use your solution for now.

    Thanks
     



  • @Laertes said:

    You got a valid point threre.

    But your solution will also grow with the amount of preferences; although linear not exponential as in my case ;)

    I wonder if there is a more generic way to do this, a solution that doesnt grow at all.
    Like getting all critical preferences in a set, looping through all of them and build the string.
    But to keep it generic, i'd need to get to the names of the variables somehow...

    Well listen to me thinking aloud ;)
    Nevermind, i'll use your solution for now.

    Thanks
     

    sb[]?

    for (i=1; i<sbArgs; ++i)

    {

          if (sb[i] == null)

    //etc

    }

    I messed up but you see my point. throw your strings into an array of strings and then build your error string from that.



  • public SomeClass(HashMap<String, String> configMap, PARAMS) throws CriticalPreferenceMissingException {
            if(configMap.isEmpty()) {
                throw new IllegalArgumentException("Configuration HashMap can't be emty.");
            }
                for (i=0; i <=PARAMS; ++i){

                      string[i] = configMap.get("string"+i.toChar()) //or however you'd do this, not my business...

                      if (string[i] == null){

                      //put your stringbuilder here

                      }

                }


                //string1 = configMap.get("string1");
                //string2 = configMap.get("string2");
                //string3 = configMap.get("webappRoot");
                integer1 = Integer.parseInt(configMap.get("integer1"));
                boolean1 = Boolean.parseBoolean(configMap.get("boolean1"));
                
                /* if(string1 == null || string2 == null || string3 == null) {
                    int errorCode =
                        string1 == null ? 1 : 0 +
                        string2 == null ? 2 : 0 +
                        string3 == null ? 4 : 0;
                   
                    switch (errorCode) {
                        case 1:
                            throw new CriticalPreferenceMissingException("string1 not set.");
                        case 2:
                            throw new CriticalPreferenceMissingException("string2 not set.");
                        case 3:
                            throw new CriticalPreferenceMissingException("string1 and string2 not set.");
                        case 4:
                            throw new CriticalPreferenceMissingException("string3 not set");
                        case 5:
                            throw new CriticalPreferenceMissingException("string1 andstring3 not set");
                        case 6:
                            throw new CriticalPreferenceMissingException("string3 and string2 not set");
                        case 7:
                            throw new CriticalPreferenceMissingException("string1, string2 and string3 not set");
                    }
                } */
        }



  • stupid edit timers.

    In my for loop at the end put

     if (string[i] == null){

                      //put your stringbuilder here

                      // ie sb.append ("  String" + i + " ")

    }

    and then right outside put

    if  (sb <> null){

                      throw new CriticalPreferenceMissingException("Missing: " + sb)

    }



  • I see, thanks!



  • @Laertes said:

    I see, thanks!

    You're welcome. Now i just want AmmoQ to give me props. Cause he's my idol. :-)



  • @GeneWitch said:

    You're welcome. Now i just want AmmoQ to give me props. Cause he's my idol. :-)

    Why?



  • @ammoQ said:

    Why! Go away! I don't want your love! Leave me alone! >: (

    ;) 



  • @dhromed said:

    @ammoQ said:

    Why! Go away! I don't want your love! Leave me alone! >: (

    ;) 

    ggg

    Actualy, something went wrong with my previous post. It should read: "Why doesn't everybody appreciate my supremacy like you?", but somehow, the forum software ate all but the first word. ;-) 


Log in to reply