Archive for August, 2007

Web hosting solutions - 826 Exception Handling Chapter 14 If an exception

Tuesday, August 21st, 2007

826 Exception Handling Chapter 14 If an exception occurs, the program skips the rest of the try block. If the program catches the exception in one of the catchhandlers, the program processes the exception. Then the finallyblock releases the resource, and control then proceeds to the first statement after the finallyblock. If an exception that occurs in the try block cannot be caught by one of the catch handlers, the program skips the rest of the tryblock and control proceeds to the finally block, which releases the resource. Then the program passes the exception up the call chain until some calling method chooses to catchit. If no method chooses to deal with it, a non- GUI-based application terminates. If a catchhandler throws an exception, the finallyblock still executes. Then the exception is passed up the call chain for a calling method to catchand handle. The Java application of Fig. 14.9 demonstrates that the finally block (if one is present) executes even if an exception is not thrown in the corresponding tryblock. The program contains methods main (lines 7 21), throwException (lines 24 50) and doesNotThrowException (lines 53 75). Methods throwException and does- NotThrowException are declared static so main (another static method) can call them directly. Method main begins executing, enters its try block and immediately calls throwException(line 11). Method throwExceptionthrows an Exception(line 29), catches it (line 33) and rethrows it (line 37). The rethrown exception will be handled in main, but first the finally block (lines 44 47) executes. Method main detects the rethrown exception in the try block in main(lines 10 12) and handles it by the catch block (lines 15 18). Next, main calls method doesNotThrowException (line 20). No exception is thrown in doesNotThrowException s try block, so the program skips the catch block (lines 61 64), but the finally block (lines 68 61) nevertheless executes. Control proceeds to the statement after the finallyblock. Then control returns to mainand the program terminates. 1 // Fig. 14.9: UsingExceptions.java 2 // Demonstration of the try-catch-finally 3 // exception handling mechanism. 4 public class UsingExceptions { 5 6 // execute application 7 public static void main( String args[] ) 8 { 9 // call method throwException 10 try { 11 throwException(); 12 } 13 14 // catch Exceptions thrown by method throwException 15 catch ( Exception exception ) 16 { 17 System.err.println( “Exception handled in main” ); 18 } 19 Fig. 14.9 Demonstration of the try-catch-finallyexception-handling mechanism (part 1 of 3).
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Chapter 14 Exception Handling 825 Testing and Debugging

Tuesday, August 21st, 2007

Chapter 14 Exception Handling 825 Testing and Debugging Tip 14.9 Catching subclass exception objects individually is subject to error if the programmer forgets to test for one or more of the subclass types explicitly; catching the superclass guarantees that objects of all subclasses will be caught. Often, a catch of the superclass type follows all other subclass exception handlers to ensure that all exceptions are processed properly. 14.13 finally Block Programs that obtain certain types of resources must return those resources to the system explicitly to avoid so-called resource leaks. In programming languages like C and C++, the most common kind of resource leak is a memory leak. Java performs automatic garbage collection of memory no longer used by programs, thus avoiding most memory leaks. However, other types of resource leaks can occur in Java. Software Engineering Observation 14.14 A finally block typically contains code to release resources acquired in its corresponding try block; this is an effective way to eliminate resource leaks. For example, the finally block should close any files opened in the try block. Testing and Debugging Tip 14.10 Actually, Java does not eliminate memory leaks completely. There is a subtle issue here. Java will not garbage collect an object until there are no more references to the object. Thus, memory leaks can occur, but only if programmers erroneously keep references to unwanted objects. Most memory leak problems are solved by Java s garbage collection. The finally block is optional. If it is present, it is placed after the last of a try block s catch blocks, as follows: try { statements resource-acquisition statements } catch ( AKindOfException exception1 ) { exception-handling statements } catch ( AnotherKindOfException exception2 ) { exception-handling statements } finally { statements resource-release statements } Java guarantees that a finally block (if one is present) will execute regardless of whether any exception is thrown in the corresponding try block or any of its corresponding catch blocks. Java also guarantees that a finally block (if one is present) will execute if a try block exits via a return, breakor continue statement. Resource-release code is placed in a finally block. Suppose a resource is allocated in a try block. If no exception occurs, the catch handlers are skipped and control proceeds to the finally block, which frees the resource. Control then proceeds to the first statement after the finally block.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

824 Exception Handling Chapter 14 TheTheTheThejava.netpackage exceptionspackage exceptionspackage (Free web hosts)

Monday, August 20th, 2007

824 Exception Handling Chapter 14 TheTheTheThejava.netpackage exceptionspackage exceptionspackage exceptionspackage exceptions Exception IOException BindException MalformedURLException ProtocolException SocketException ConnectException NoRouteToHostException UnknownHostException UnknownServiceException Fig. 14.8The java.netpackage exceptions. 14. 14.11 Constructors, Finalizers and Exception Handling First, let us deal with an issue we have mentioned, but that has yet to be resolved satisfactorily. What happens when an error is detected in a constructor? The problem is that a constructor cannot return a value, so how do we let the program know that an object has not been constructed properly? One scheme is to return the improperly constructed object and hope that anyone using the object would perform appropriate tests to determine that the object is in fact bad. However, this directly contradicts discussions in Chapter 8 in which we indicated that you should maintain an object in a consistent state at all times. Another scheme is to set some instance variable with an error indicator outside the constructor, but this is a poor programming practice. In Java, the typical (and proper) mechanism is to throw an exception from the constructor to the code creating the object. The thrown object contains the information about the failed constructor call and the caller is responsible for handling the failure. When an exception occurs in a constructor, other objects created by that constructor are marked for eventual garbage collection. Before each object is garbage collected, its finalizemethod will be called. 14.12 Exceptions and Inheritance Various exception classes can be derived from a common superclass. If a catchis written to catch exception objects of a superclass type, it can also catch all objects of subclasses of that superclass. This can allow for polymorphic processing of related exceptions. Using inheritance with exceptions enables an exception handler to catch related errors with a concise notation. One could certainly catch each subclass exception object individually if those exceptions require different processing, but it is more concise to catch the superclass exception object. Of course, this makes sense only if the handling behavior would be the same for all subclasses. Otherwise, catch each subclass exception individually.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Chapter 14 Exception Handling 823 The java.iopackage exceptions (Web hosting india)

Monday, August 20th, 2007

Chapter 14 Exception Handling 823 The java.iopackage exceptions Exception IOException CharConversionException EOFException FileNotFoundException InterruptedIOException ObjectStreamException InvalidClassException InvalidObjectException NotActiveException NotSerializableException OptionalDataException StreamCorruptedException WriteAbortedException SyncFailedException UnsupportedCodingException UTFDataFormatException Fig. 14.6 The java.iopackage exceptions . TheTheTheThejava.awtpackage exceptionspackage exceptionspackage exceptionspackage exceptions Exception AWTException RuntimeException IllegalStateException IllegalComponentStateException Fig. 14.7 The java.awt package exceptions . Figure 14.8 lists the IOExceptions of the java.net package. These are all checked Exceptions that indicate various networking problems. Most packages in the Java API define Exceptions and Errors specific to the package. For a complete list of these types, see the online API documentation for the package.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

822 Exception Handling (Web design seattle) Chapter 14 The java.langpackage exceptions

Sunday, August 19th, 2007

822 Exception Handling Chapter 14 The java.langpackage exceptions RuntimeException ArithmeticException ArrayStoreException ClassCastException IllegalArgumentException IllegalThreadStateException NumberFormatException IllegalMonitorStateException IllegalStateException IndexOutOfBoundsException ArrayIndexOutOfBoundsException StringIndexOutOfBoundsException NegativeArraySizeException NullPointerException SecurityException Fig. 14.4 The java.langpackage exceptions (part 2 of 2). Figure 14.5 lists Java s other three RuntimeExceptions data types. We will encounter these exceptions in Chapter 20 when we study the Vectorclass. A Vectoris a dynamic array that can grow and shrink to accommodate a program s varying storage requirements. Figure 14.6 lists Java s IOExceptions. These are all checked exceptions that can occur during input/output and file processing. Figure 14.7 lists the java.awt package s only checked Exception, the AWTException. This is a checked exception that is thrown by various abstract windowing toolkit methods. The java.utilpackage exceptions Exception RuntimeException EmptyStackException MissingResourceException NoSuchElementException TooManyListenersException Fig. 14.5 The java.utilpackage exceptions.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Chapter 14 Exception Handling 821 The java.langpackage errors (Free web hosting with ftp)

Sunday, August 19th, 2007

Chapter 14 Exception Handling 821 The java.langpackage errors Error(all in java.langexcept for AWTError, which is in java.awt) LinkageError ClassCircularityError ClassFormatError ExceptionInInitializerError IncompatibleClassChangeError AbstractMethodError IllegalAccessError InstantiationError NoSuchFieldError NoSuchMethodError NoClassDefFoundError UnsatisfiedLinkError VerifyError ThreadDeath VirtualMachineError (Abstract class) InternalError OutOfMemoryError StackOverflowError UnknownError AWTError(package java.awt) Fig. 14.3 The java.langpackage errors . The java.langpackage exceptions Exception ClassNotFoundException CloneNotSupportedException IllegalAccessException InstantiationException InterruptedException NoSuchFieldException NoSuchMethodException Fig. 14.4 The java.langpackage exceptions (part 1 of 2).
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

820 Exception Handling Chapter 14 Java s catch-or-declare requirement (Starting a web site)

Sunday, August 19th, 2007

820 Exception Handling Chapter 14 Java s catch-or-declare requirement demands that the programmer either catcheach checked exception or place it in the throws clause of a method. Of course, placing a checked exception in the throws clause would force other methods to process the checked exception as well. If a programmer feels a particular checked exception is unlikely to occur, the programmer might elect to catch that checked exception and do nothing with it to avoid being forced to deal with it later. This can of course come back to haunt you, because as a program evolves, it may become important to deal with this checked exception. Testing and Debugging Tip 14.6 Do not try to circumvent Java s catch-or-declare requirement by simply catching exceptions and doing nothing with them. Exceptions are generally of a serious enough nature that they need to be processed rather than suppressed. Testing and Debugging Tip 14.7 The Java compiler, through the throws clause used with exception handling, forces programmers to process the exceptions that can be thrown from each method a program calls. This helps avoid bugs that arise in programs when programmers ignore the fact that problems occur and make no provisions for these problems. Software Engineering Observation 14.13 Subclass methods that do not override their corresponding superclass methods exhibit the same exception-handling behavior of the inherited superclass methods. The throws clause of a subclass method that overrides a superclass method may contain the same list of exceptions as the overridden superclass method or a subset of that list. Common Programming Error 14.12 The Java compiler requires that a method either catch any checked exceptions thrown in the method (either directly from the method s body or indirectly through called methods) or declare checked Exceptions the method can throw to other methods; otherwise, the Java compiler issues a syntax error. Testing and Debugging Tip 14.8 Suppose a method throws all subclasses of a particular exception superclass. You may be tempted to list only the superclass in the throws clause. Instead, explicitly list all the subclasses. This focuses the programmer s attention on the specific Exceptions that may occur and will often help avoid bugs caused by performing general processing for a category of exception types. Figure 14.3 Fig. 14.8 list many of Java s Errors and Exceptions hierarchically for the packages java.lang, java.util, java.io, java.awt and java.net. In these tables, a class indented under another class is a subclass. The exception and error classes for the other packages of the Java API can be found in the Java online documentation. The online documentation for each method in the API specifies whether that method throws exceptions and what the exceptions are that can be thrown. We show a portion of Java s Error hierarchy in Fig. 14.3. Most Java programmers ignore Errors. They are serious but rare events. Figure 14.4 is particularly important because it lists many of Java s Runtime- Exceptions. Although Java programmers are not required to declare these exceptions in throws clauses, these are the exceptions that commonly will be caught and handled in Java applications.
We recommend high quality webhost to host and run your jsp application: christian web host services.

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

Saturday, August 18th, 2007

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.

Web server setup - 818 Exception Handling Chapter 14 of method quotientin

Saturday, August 18th, 2007

818 Exception Handling Chapter 14 of method quotientin the try block (line 65). Line 66 displays the result of the calculation in the third JTextField. In this case, the try block completes successfully, so the program skips the catch blocks and the actionPerformed method completes execution normally. Note that when quotient throws the DivideByZeroException, quotient s block expires (i.e., the method terminates). This would cause any of its local variables to be destroyed objects that were referenced by local variables in the block would have their reference counts decremented accordingly (and are possibly marked for garbage collection). Also, the try block from which the method was called expires before line 66 can execute. Here, too, if there were local variables created in the tryblock prior to the exception being thrown, these variables would be destroyed. If a NumberFormatExceptionis generated by lines 62 63, the try block expires and execution continues with the exception handler at line 70, which displays an error message to tell the user to input integers. Then the actionPerformedmethod continues with the next valid statement after the catch blocks (i.e., the method terminates in this example). 14.9 Rethrowing an Exception It is possible that the catchhandler that catches an exception may decide it cannot process the exception, or it may want to let some other catch handler handle the exception. In this case, the handler that received the exception can rethrow the exception with the statement throw exceptionReference; where exceptionReference is the parameter name for the exception in the catch handler. Such a throw rethrows the exception to the next enclosing try block. Even if a handler can process an exception, and regardless of whether it does any processing on that exception, the handler still can rethrow the exception for further processing outside the handler. A rethrown exception is detected by the next enclosing try block and is handled by an exception handler listed after that enclosing try block. 14.10 throws Clause A throws clause lists the exceptions that can be thrown by a method as in int functionName( parameterList ) throws ExceptionType1, ExceptionType2, ExceptionType3, { // method body } The types of exceptions that are thrown by a method are specified in the method definition with a comma-separated list in the throws clause. A method can throw objects of the indicated classes, or it can throw objects of their subclasses. Some exceptions can occur at any point during the execution of the program. Many of these exceptions can be avoided by coding properly. These are run-time exceptions, and they derive from class RuntimeException. For example, if your program attempts to access an out-of-range array subscript, an exception of type ArrayIndexOutOf- BoundsException (derived from RuntimeException) occurs. Your program clearly can avoid such a problem; hence, it is a run-time exception.
We recommend high quality webhost to host and run your jsp application: christian web host services.

Hosting your own web site - Chapter 14 Exception Handling 817 When the user

Friday, August 17th, 2007

Chapter 14 Exception Handling 817 When the user inputs the denominator and presses the Enter key, the program calls method actionPerformed (lines 54 84). Next, method actionPerformed proceeds with a try block (lines 61 67), which encloses the code that may throw an exception and any code that should not be executed if an exception occurs. The statements that read the integers from the JTextFields (lines 62 63) each use method Integer.parseInt to convert Strings to int values. Method parseInt throws a NumberFormatException if its String argument is not a valid integer. The division that can cause the divide-by-zero error is not performed explicitly in the try block. Rather, the call to method quotient (line 65) invokes the code that attempts the division. Method quotient (lines 89 96) throws the DivideByZeroException object, as we will see momentarily. In general, errors may surface through explicitly mentioned code in a tryblock, through calls to a method or even through deeply nested method calls initiated by code in a try block. The tryblock in this example is followed by two catch blocks lines 70 75 contain the exception handler for the NumberFormatException and lines 78 83 contain the exception handler for the DivideByZeroException. In general, when the program detects an exception while executing a try block, the program catches the exception in a catch block that specifies an appropriate exception type (i.e., the type in the catch matches the thrown exception type exactly or is a superclass of the thrown exception type). In Fig. 14.2, the first catch block specifies that it will catch exception objects of type NumberFormatException (this type matches the exception object type thrown in method Integer.parseInt) and the second catch block specifies that it will catch exception objects of type ArithmeticException (this type is a superclass of the exception object type thrown in method quotient). Only the matching catch handler executes when an exception occurs. Both our exception handlers simply display an error- message dialog, but exception handlers can be more elaborate than this. After executing an exception handler, program control proceeds to the first statement after the last catch block (or in the finally block, if one is present). If the code in the tryblock does not throw an exception, then the catch handlers are skipped and execution resumes with the first line of code after the catch handlers (or in the finally block, if one is present). In Fig. 14.2, method actionPerformed simply returns, but the program could continue executing more statements after the catch blocks. Testing and Debugging Tip 14.5 With exception handling, a program can continue executing after dealing with a problem. This helps ensure robust applications that contribute to what is called mission-critical computing or business-critical computing. Now let us examine method quotient(lines 89 96). When the if structure determines that denominator is zero, the body of the if executes a throw statement that creates and throws a new DivideByZeroException object. This object will be caught by the catch block (lines 78 83) specifying type ArithmeticException after the try block. The catch block specifies parameter name arithmeticException to receive the thrown exception object. The ArithmeticException handler converts the exception to a Stringvia toString and passes this String as the message to display in an error-message dialog. If denominator is not zero, quotient does not throw an exception. Rather, quotient performs the division and returns the result of the division to the point of invocation
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.