Chapter 14 (Web host server) Exception Handling 819 Another run-time exception

Chapter 14 Exception Handling 819 Another run-time exception occurs when your program creates an object reference, but has not yet created an object and assigned it to the reference. Attempting to use such a null reference causes a NullPointerException to be thrown. Clearly, your program can avoid this circumstance; hence, it is a run-time exception. Another run-time exception is an invalid cast, which throws a ClassCastException. There are a variety of exceptions that are not RuntimeExceptions. Two of the most common are InterruptedExceptions (see Chapter 15, Multithreading ) and IOExceptions (see Chapter 16, Files and Streams ). Not all errors and exceptions that can be thrown from a method are required to be listed in the throws clause. Errors do not need to be listed, nor do RuntimeExceptions (avoidable exceptions). Errors are serious system problems that can occur almost anywhere, and most programs will not be able to recover from them. Methods should process RuntimeExceptions caught in their bodies directly rather than passing them on to other program components. If a method throws any non-RuntimeExceptions, it must specify those exception types in its throws clause. Software Engineering Observation 14.11 If a non-RuntimeException is thrown by a method, or if that method calls methods that throw non-RuntimeExceptions, each of those exceptions must be declared in the throws clause of that method or caught in a try/catch in that method. Java distinguishes checked Exceptions versus unchecked RuntimeExceptions and Errors. A method s checked exceptions need to be listed in that method s throws clause. Errors and RuntimeExceptions can be thrown from almost any method, so it would be cumbersome for programmers to be required to list them for every method definition. Such exceptions and errors are not required to be listed in a method s throws clause and, hence, are said to be unchecked by the compiler. All non-RuntimeExceptions a method can throwmust be listed in that method s throws clause and, hence, are said to be checked by the compiler. If a non-RuntimeExceptionis not listed in the throws clause, the compiler will issue an error message indicating that the exception must be caught (with a try/catch in the body of the method) or declared (with a throws clause). Common Programming Error 14.9 It is a syntax error if a method throws a checked exception not in that method s throws clause. Common Programming Error 14.10 Attempting to throw a checked exception from a method that has no throws clause is a syntax error. Software Engineering Observation 14.12 If your method calls other methods that explicitly throw checked exceptions, those exceptions must be listed in the throws clause of your method, unless your method catches those exceptions. This is Java s catch-or-declare requirement. Common Programming Error 14.11 If a subclass method overrides a superclass method, it is an error for the subclass method to list more exceptions in its throws list than the overridden superclass method does. A subclass s throws list can contain a subset of a superclass s throws list.
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

Leave a Reply