Problem

Guidelines for Exception HandlingSourceforge.net publishes guidelines for throwing and cat...

Guidelines for Exception Handling

Sourceforge.net publishes guidelines for throwing and catching exceptions. Following are seven examples and seven explanations from Sourceforge.net. Associate each example with its proper explanation.

Examples:

a. public void methodThrowingException() throws Exception {}


b. public class Foo   {    public void bar()    {     try     {       // do something     }     catch (Throwable th)     {     }    }   }


c. public class Foo   {     void bar()     {       try       {         try         {         }         catch (Exception e)         {           throw new WrapperException(e);         }       }       catch (WrapperException e)       {         // do some more stuff       }     }   }


d. public class Foo   {     void bar()     {       try       {         // do something       }       catch (NullPointerException npe)       {       }     }   }
e. public class Foo   {     public void bar() throws Exception     {       throw new Exception();     }   }
f. public class Foo   {     void bar()     {       throw new NullPointerException();     }   }
g. public class Foo   {     void bar()      {        try        {          // do something        }        catch (SomeException se)        {          throw se;        }      }   }

Explanations:

1. Do not catch a NullPointerException. A NullPointerException is a sign of serious bugs in your code. A catch block may obscure the original error, causing other more subtle errors in its wake.


2. Avoid using a method signature that throws Exception. It might be difficult to document and understand the vague interfaces. Use either a class derived from RuntimeException or a checked exception.


3. Avoid oid catching T hrowable exceptions. It casts too wide a net—catching things like OutOfMemoryError.


4. Do not use exception catching as flow control. Using exceptions as flow control leads to GOTOish code and obscures true exceptions when debugging.


5. Avoid rethrowing a caught exception. Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity. There are times when catching an exception and rethrowing a new exception is appropriate.


6. Try not to throw “raw” exception types. Rather than throw a raw RuntimeException, Throwable, Exception, or Error, use a subclassed exception or error instead.


7. Avoid throwing a NullPointerException. People will assume that the JVM threw the exception. Consider using an IllegalArgumentException instead; this will be clearly seen as a programmer-initiated exception.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 14
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT