Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Exceptions to the programming rules, Part 2

Incorporate Java's throw-object/catch-object exception-handling technique into your Java programs

  • Print
  • Feedback

Page 4 of 8

  • Suppose you design a mathematics library that alerts programs to overflow and underflow exceptions. To represent those exceptions, you introduce Overflow and Underflow classes. Because overflow and underflow exceptions indicate flawed code, they represent unchecked exceptions. That implies Overflow and Underflow subclass RuntimeException, as the following code fragment demonstrates:

    class Overflow extends RuntimeException
    {
    }
    class Underflow extends RuntimeException
    {
    }
    


    Because Overflow and Underflow lack duplicate code, you neither need to introduce a new class that extends RuntimeException nor have Overflow and Underflow subclass that new class.



  • Throw exception objects

    The previous section referred to throwing an exception object. What does that phrase mean? For an answer, consider this: When the JVM detects an unchecked exception, the JVM creates and initializes an exception object. In contrast, when a program or library detects either a checked or unchecked exception, the program/library code creates and initializes an exception object and uses the throw statement to pass that object to the JVM. The throw statement has the following syntax:

    • Print
    • Feedback

    Resources