Rethrowing an Exception Tutorial

There may be occasions where you want to catch an exception, then determine if it can be handled properly and if not, rethrow the exception to be handled further down the call stack. The example code for this tutorial will create a method that expands the capabilities of the parseInt() method in the Integer class.
Consider the following:
int stringToInt(String s) {
        int i = 0;
        try {
                i = Integer.parseInt(s);
        } catch (NumberFormatException e) {
                switch (s.toUpperCase()) {
                       case "ONE":
                             return 1;
                             ...
               }
               throw e;
        }
        return i;
}

This method can take arguments like "1", "one", "One", "oNe", etc. and convert them all to a primitive int value.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ExceptionRethrow
C:\Java>cd ExceptionRethrow
C:\Java\ExceptionRethrow>Notepad ExceptionRethrow.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ExceptionRethrow {
    public static void main(String args[]) {
        displayOurInt("zero");
        displayOurInt("one");
        displayOurInt("Two");
        displayOurInt("THREE");
        displayOurInt("fouR");
        displayOurInt("FiVE");
        displayOurInt("six");
        displayOurInt("7");
        displayOurInt("seven");
        displayOurInt("8");
        displayOurInt("Eight");
        displayOurInt("9");
        displayOurInt("nInE");
        displayOurInt("TEN");
        displayOurInt("sixty");
    }

    static void displayOurInt(String parseIt) {
        int i = 0;
        try {
            i = stringToInt(parseIt);
            System.out.println(i);
        } catch (NumberFormatException e) {
            System.out.println("Unable to convert " + parseIt + " to a primitve int");
        }
    }

    static int stringToInt(String s) {
        int i = 0;
        try {
            i = Integer.parseInt(s);
        } catch (NumberFormatException e) {
            switch (s.toUpperCase()) {
                case "ONE":
                    return 1;
                case "TWO":
                    return 2;
                case "THREE":
                    return 3;
                case "FOUR":
                    return 4;
                case "FIVE":
                    return 5;
                case "SIX":
                    return 6;
                case "SEVEN":
                    return 7;
                case "EIGHT":
                    return 8;
                case "NINE":
                    return 9;        
            }
            throw e;
        }
        return i;
    }
}

Now switch back to the command prompt (CMD) and type in javac ExceptionRethrow.java and press Enter.
Now type in java ExceptionRethrow and press Enter.


C:\Java\ExceptionRethrow>javac ExceptionRethrow.java
C:\Java\ExceptionRethrow>java ExceptionRethrow
Unable to convert zero to a primitive int
1
2
3
4
5
6
7
7
8
8
9
9
Unable to convert TEN to a primitive int
Unable to convert sixty to a primitive int



Final thoughts

If you have been following my tutorial series, you may have noticed that there are quite a few tutorials on Exception Handling. Exception handling is an important topic and quite frankly – IMO – it is not given enough attention by many in the Java community. I plan on making a few more tutorials on exception handling before I move on to another subject.


Tutorials