Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 142 Object-Oriented Programming I More on Exceptions Checked vs. unchecked exceptions There are two kinds of exceptions in Java, checked and unchecked, and only checked exceptions need appear in throws clauses. The general rule is: Any checked exceptions that may be thrown in a method must either be caught or declared in the method's throws clause. Checked exceptions are so called because both the Java compiler and the Java virtual machine check to make sure this rule is obeyed. Whether or not an exception is "checked" is determined by its position in the hierarchy of throwable classes. The diagram below shows that some parts of the Throwable family tree contain checked exceptions while other parts contain unchecked exceptions. To create a new checked exception, you simply extend another checked exception. All throwables that are subclasses of Exception, but not subclasses of RuntimeException are checked exceptions. for non-color print out Most unchecked throwables declared in java.lang (subclasses of Error and RuntimeException) are problems that would be detected by the Java virtual machine. Errors usually signal abnormal conditions that a program should not handle. Problems with linking, such as NoClassDefFoundError, or memory, such as StackOverflowError, could happen just about anywhere in a program. In the rare cases in which they happen, it is usually reasonable that the thread terminate. Although most runtime exceptions (members of the RuntimeException family) also are thrown by the Java virtual machine itself, they usually are more an indication of software bugs. Problems with arrays, such as ArrayIndexOutOfBoundsException, or passed parameters, such as IllegalArgumentException, also could happen just about anywhere in a program. When exceptions like these are thrown, it is better to fix the bugs that caused them to be thrown. Otherwise every invocation of a method that uses arrays would require a catch clause for ArrayIndexOutOfBoundsException. Page 1 of 2 CS 142 Object-Oriented Programming I Guidelines In summary, a checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. An unchecked exception is a RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked, but as you should be focusing on throwing exceptions only, your decision should be whether to throw a subclass of RuntimeException (an unchecked exception) or some other subclass of Exception (a checked exception). If you throw a checked exception (and don't catch it), you will need to declare the exception in your method's throws clause. If you throw an unchecked exception, you can decide whether to catch or disregard the exception, just as with checked exceptions. In general, exceptions that indicate an improper use of a class should be unchecked. The StringIndexOutOfBoundsException thrown by String's charAt() method is an unchecked exception. The designers of the String class didn't want to force programmers to deal with the possibility of an invalid index parameter every time they called charAt(int index). The read() method of class java.io.FileInputStream, on the other hand, throws IOException, which is a checked exception. This exception indicates some kind of error occurred while attempting to read from the file. It doesn't indicate that the programmer has used the FileInputStream class improperly. It just signals that the method itself is unable to fulfill its contractual responsibility of reading in the next byte from the file. The designers of the FileInputStream class considered this abnormal condition to be common enough, and important enough, to force programmers to deal with it. If a method includes code that could cause a checked exception to be thrown, then: the exception must be declared in the method header, using a throws clause, or the code that might cause the exception to be thrown must be inside a try block with a catch clause for that exception Writing new Exception class When a program encounters an exceptional condition and has no way of handling it immediately, the program can throw an exception. In some cases, it makes sense to throw an exception belonging to one of Java's predefined classes, such as IllegalArgumentException or IOException. However, if there is no standard class that adequately represents the exceptional condition, the programmer can define a new exception class. The new class must extend the standard class Throwable or one of its subclasses. In general, the new class will extend RuntimeException (or one of its subclasses) if the programmer does not want to require mandatory exception handling. To create a new exception class that does require mandatory handling, the programmer can extend one of the other subclasses of Exception or can extend Exception itself. public class MyError extends Exception { public MyError(String message) { //constructor-create MyError object with given message as its error message super(message); } } class extends Exception and therefore requires mandatory exception handling (checked) Page 2 of 2