|
|
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
Page 7 of 7
ExcDemo, Stack's Parent instance inner class extends Exception -- a subclass of Throwable. In excdemo, Parent extends no class.
ExcDemo, Stack's push() and pop() method signatures include throws clauses. Those clauses identify the types of exception objects that those methods throw. excdemo has no equivalent clauses.
ExcDemo, the throw statements use keyword new to create the exception objects. excdemo's throw statements include no such syntax.
Before leaving this section, consider the following difference between the C, C++, and Java exception-handling techniques: Unlike C and C++, Java allows you to detect and handle exceptions that arise from flawed code. For example, suppose you write a C++ program that contains the following code fragment:
int y = 0; int x = 1; cout << x / y;
The code fragment attempts to divide 1 by 0 and output the result. When the code fragment runs under an operating system like Windows 98, the operating system forcefully terminates the fragment's program. Now, suppose you incorporate the following Java code fragment into a Java program:
int y = 0; int x = 1; System.out.println (x / y);
Upon encountering x / y, the JVM creates an object from java.lang.ArithmeticException, initializes that object with information regarding the division by zero attempt, and searches for a catch clause that can handle that exception type. You will learn more about Java's ability to handle flawed code-based exceptions
in Part 2.
Dealing with exceptions is an important issue in software development. Programs must handle exceptions or subject themselves to different kinds of failure. This article introduced you to exceptions and explored how to handle them in C, C++, and Java. You learned that C programs handle exceptions via error codes and error code testing. Because the problems with error code testing prove more troublesome in larger programs, a better technique for handling exceptions arose: throwing and catching objects that describe exceptions. Although the throw-object/catch-object technique originally appeared in C++, that technique has migrated into Java. You received a taste of that technique by contrasting the C++ and Java versions of programs that handle exceptions with the throw-object/catch-object technique.
I encourage you to email me with any questions you might have involving either this or any previous article's material. (Please keep such questions relevant to material discussed in this column's articles.) Your questions and my answers will appear in the relevant study guides.
Part 2 will conclude our investigation of exceptional programming. It will focus on Java's features for designing new exception classes, throwing exceptions, catching exceptions, and performing cleanup operations on your code -- whether or not that code throws an exception object.