Archive for August, 2007

Web hosting colocation - 836 Exception Handling Chapter 14 14.11 Provide a

Monday, August 27th, 2007

836 Exception Handling Chapter 14 14.11 Provide a single Exception subclass and catch handler for a group of exceptions. As each exception occurs, the exception object can be created with different instance data. The catch handler can examine this data to distinguish the type of the Exception. 14.12 The finally block is the preferred means for preventing resource leaks. 14.13 No, but it does terminate the block in which the Exception is thrown. 14.14 The exception will be processed by a catch handler (if one exists) associated with the try block (if one exists) enclosing the catch handler that caused the exception. 14.15 The reference is removed from memory, and the reference count for the referenced object is decremented. If the reference count is zero, the object is marked for garbage collection. EXERCISES 14.16 Under what circumstances would you use the following catch handler? catch ( Exception exception ) { throw exception; } 14.17 List the benefits of exception handling over conventional means of error processing. 14.18 Describe an object-oriented technique for handling related exceptions. 14.19 Until this chapter, we have found that dealing with errors detected by constructors is a bit awkward. Explain why exception handling is an effective means for dealing with constructor failure. 14.20 Suppose a program throws an exception and the appropriate exception handler begins executing. Now suppose that the exception handler itself throws the same exception. Does this create an infinite recursion? Explain your answer. 14.21 Use inheritance to create an exception superclass and various exception subclasses. Write a program to demonstrate that the catch specifying the superclass catches subclass exceptions. 14.22 Write a Java program that shows that not all finalizers for objects constructed in a block are necessarily called after an exception is thrown from that block. 14.23 Write a Java program that demonstrates how various exceptions are caught with catch ( Exception exception ) 14.24 Write a Java program that shows that the order of exception handlers is important. If you try to catch a superclass exception type before a subclass type, the compiler should generate errors. Explain why these errors occur. 14.25 Write a Java program that shows a constructor passing information about constructor failure to an exception handler after a tryblock. 14.26 Write a Java program that illustrates rethrowing an exception. 14.27 Write a Java program that shows that a method with its own try block does not have to catch every possible error generated within the try. Some exceptions can slip through to, and be handled in, other scopes.
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Web hosting solutions - Chapter 14 Exception Handling 835 14.5 If no

Sunday, August 26th, 2007

Chapter 14 Exception Handling 835 14.5 If no exceptions are thrown in a try block, where does control proceed to when the try block completes execution? 14.6 What happens if an exception occurs and an appropriate exception handler cannot be found? 14.7 Give a key advantage of using catch(Exception e). 14.8 Should a conventional applet or application catch Error objects? 14.9 What happens if several handlers match the type of the thrown object? 14.10 Why would a programmer specify a superclass type as the type of a catch handler, then throw objects of subclass types? 14.11 How might a catch handler be written to process related types of errors without using inheritance among exception classes? 14.12 What is the key reason for using finally blocks? 14.13 Does throwing an Exceptionhave to cause program termination? 14.14 What happens when a catch handler throws an Exception? 14.15 What happens to a local reference in a tryblock when that block throws an Exception? ANSWERS TO SELF-REVIEW EXERCISES 14.1 Memory exhaustion, array subscript out of bounds, arithmetic overflow, division by zero, invalid method parameters. 14.2 (a) Exception handling is designed to handle infrequently occurring situations that often result in program termination, so compiler writers are not required to implement exception handling to perform optimally. (b) Flow of control with conventional control structures is generally clearer and more efficient than with exceptions. (c) Problems can occur because the stack is unwound when an exception occurs and resources allocated prior to the exception may not be freed. (d) The additional exceptions can get in the way of genuine error-type exceptions. It becomes more difficult for the programmer to keep track of the larger number of exception cases. 14.3 It is unlikely that library classes and methods could perform error processing that would meet the unique needs of all users. 14.4 A resource leak occurs when an executing program does not properly release a resource when the resource is no longer needed. If the program attempts to use the resource again in the future, the program may not be able to access the resource. 14.5 The exception handlers (in the catch blocks) for that try block are skipped, and the program resumes execution after the last catch block. If there is a finally block, it is executed and the program resumes execution after the finallyblock. 14.6 A non-GUI-based application terminates; an applet or a GUI-based application resumes regular event processing. 14.7 The form catch(Exception e) catches any type of exception thrown in a try block. An advantage is that no thrown Exception can slip by. 14.8 Errors are usually serious problems with the underlying Java system; most programs will not want to catch Errors. 14.9 The first matching Exception handler after the try block is executed. 14.10 This is a nice way to catch related types of exceptions, but it should be used carefully.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

834 Exception Handling Chapter 14 handled by an (Web hosting packages)

Sunday, August 26th, 2007

834 Exception Handling Chapter 14 handled by an appropriate exception handler (if there is one) listed after that enclosing try block. A throws clause lists the checked exceptions that may be thrown from a method. A method may throwthe indicated exceptions, or it may throw subclass types. If a checked exception not listed in the throwsclause is thrown, a syntax error occurs. A powerful reason for using inheritance with exceptions is to catch a variety of related errors easily with concise notation. One could certainly catch each type of subclass exception object individually, but it is more concise to simply catch the superclass exception object. TERMINOLOGY ArithmeticException instanceof operator array exceptions InstantiationException ArrayIndexOutOfBoundsException InternalException business-critical computing InterruptedException catch a group of exceptions IOException catch all exceptions library exception classes catch an exception memory exhaustion catchblock mission-critical computing catch(Exception e) NegativeArraySizeException catch-or-declare requirement NoClassDefFoundException checked Exceptions non-run-time exception ClassCastException null reference declare exceptions that can be thrown NullPointerException default exception handler OutOfMemoryError EmptyStackException printStackTrace method (Throwable) Error class resource leak Error class hierarchy resumption model of exception handling error handling rethrow an exception exception RuntimeException Exception class stack unwinding Exception class hierarchy synchronous error exception handler termination model of exception handling exception handling throw an exception exception object throw point fault tolerance throw statement FileNotFoundException Throwable class finally block throws clause getMessage method of Throwable class try block handle an exception unchecked Exceptions IllegalAccessException UnsatisfiedLinkException IncompatibleClassChangeException SELF-REVIEW EXERCISES 14.1 List five common examples of exceptions. 14.2 Why should exception-handling techniques not be used for conventional program control? 14.3 Why are exceptions particularly appropriate for dealing with errors produced by library classes and methods? 14.4 What is a resource leak?
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Personal web server - Chapter 14 Exception Handling 833 If no

Saturday, August 25th, 2007

Chapter 14 Exception Handling 833 If no exceptions are thrown in the try block, the exception handlers for that block are skipped. Then the program resumes execution after the last catch block, after executing a finally block if one is provided. Exceptions are thrown in a try block in a method or from a method called directly or indirectly from the try block. The operand of a throwcan be of any class derived from Throwable. The immediate subclasses of Throwable are Error and Exception. RuntimeExceptions and Errors are said to be unchecked. Non-RuntimeExceptions are said to be checked. The checked exceptions thrown by a particular method must be specified in that method s throws clause. Exceptions are caught by the closest exception handler (for the try block from which the exception was thrown) specifying an appropriate type. An exception terminates the block in which the exception occurred. A handler may rethrow the object to an outer tryblock. catch(Exceptionexception) catches all Exceptions. catch(Throwablethrowable) catches all Exceptions and Errors. If no handler matches a particular thrown object, the search for a match continues in an enclosing tryblock. Exception handlers are searched in order for an appropriate match based on type. The first handler that matches is executed. When that handler finishes executing, control resumes with the first statement after the last catchblock. The order of the exception handlers affects how an exception is handled. A subclass object can be caught either by a handler specifying that subclass type or by handlers specifying the types of any direct or indirect superclasses of that subclass. If no handler is found for an exception, a non-GUI-based application terminates; an applet or a GUI-based application will return to its regular event handling. An exception handler cannot access variables in the scope of its try block because by the time the exception handler begins executing, the tryblock has expired. Information the handler needs is normally passed in the thrown object. Exception handlers can rethrow an exception. They can convert one type of exception into another by throwing a different exception. They can perform any necessary recovery and resume execution after the last exception handler. They can look at the situation causing the error, remove the cause of the error and retry by calling the original method that caused an exception. They can simply return some status value to their environment. A handler that catches a subclass object should be placed before a handler that catches a superclass object. If the superclass handler were first, it would catch superclass objects and the objects of subclasses of that superclass. When an exception is caught, it is possible that resources may have been allocated, but not yet released in the try block. A finally block should release these resources. It is possible that the handler that catches an exception may decide it cannot process the exception. In this case, the handler can simply rethrow the exception. A throw followed by the exception object name rethrows the exception. Even if a handler can process an exception, and regardless of whether it does any processing on that exception, the handler can rethrow the exception for further processing outside the handler. A re- thrown exception is detected by the next enclosing tryblock (normally in a calling method) and is
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Web hosting uk - 832 Exception Handling Chapter 14 20 21 //

Saturday, August 25th, 2007

832 Exception Handling Chapter 14 20 21 // call method2; throw exceptions back to main 22 public static void method1() throws Exception 23 { 24 method2(); 25 } 26 27 // call method3; throw exceptions back to method1 28 public static void method2() throws Exception 29 { 30 method3(); 31 } 32 33 // throw Exception back to method2 34 public static void method3() throws Exception 35 { 36 throw new Exception( “Exception thrown in method3″ ); 37 } 38 39 } // end class Using Exceptions Exception thrown in method3 java.lang.Exception: Exception thrown in method3 at UsingExceptions.method3(UsingExceptions.java:36) at UsingExceptions.method2(UsingExceptions.java:30) at UsingExceptions.method1(UsingExceptions.java:24) at UsingExceptions.main(UsingExceptions.java:11)) Fig. 14.11 Using getMessageand printStackTrace(part 2 of 2). SUMMARY Some common examples of exceptions are memory exhaustion, an out-of-bounds array subscript, arithmetic overflow, division by zero and invalid method parameters. Exception handling is designed for dealing with synchronous malfunctions (i.e., those that occur as the result of a program s execution). Exception handling is used in situations in which a malfunction will be dealt with in a different scope from that which detected the malfunction. Exception handling should be used to process exceptions from software components such as methods, libraries and classes that are likely to be widely used and where it does not make sense for those components to handle their own exceptions. Exception handling should be used on large projects to handle error processing in a standardized manner for the entire project. Java exception handling is geared to situations in which the method that detects an error is unable to deal with it. Such a method will throwan exception. If the exception matches the type of the parameter in one of the catchblocks, the code for that catchblock executes. The programmer encloses in a tryblock the code that may generate an exception. The tryblock is followed by one or more catch blocks. Each catchblock specifies the type of exception it can catch and handle. Each catchblock is an exception handler.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Chapter 14 Exception Handling 831 takes the argument (Yahoo web space)

Friday, August 24th, 2007

Chapter 14 Exception Handling 831 takes the argument informationString, which is descriptive information about the Throwableobject. The informationStringstored in the Throwableobject may be obtained with method getMessage. Testing and Debugging Tip 14.14 Throwable classes have a constructor that accepts a String argument. Using this form of the constructor is helpful in determining the source of the exception via method getMessage. Figure 14.11 demonstrates getMessage and printStackTrace. Method get- Message returns the descriptive String stored in an exception. Method print- StackTrace outputs to the standard error stream (normally, the command line or console) an error message with the class name of the exception, the descriptive String stored in the exception and a list of the methods that had not completed execution when the exception was thrown (i.e., all methods currently residing on the method call stack). In the program, main invokes method1, method1 invokes method2 and method2invokes method3. At this point, the method call stack for the program is method3 method2 method1 main with the last method called (method3) at the top and the first method called (main) at the bottom. When method3 throws an Exception(line 36), a stack-trace message is generated and stored in the Exceptionobject. The stack trace reflects the throw point in the code (i.e., line 36). Then, the stack unwinds to the first method in the method call stack in which the exception can be caught (i.e., main because it contains a catch handler for Exception). The catch handler then uses getMessage and printStackTrace on the Exception object exception to produce the output. Notice that the line numbers in the output window correspond to the line numbers in the program. 1 // Fig. 14.11: UsingExceptions.java 2 // Demonstrating the getMessage and printStackTrace 3 // methods inherited into all exception classes. 4 public class UsingExceptions { 5 6 // execute application 7 public static void main( String args[] ) 8 { 9 // call method1 10 try { 11 method1(); 12 } 13 14 // catch Exceptions thrown from method1 15 catch ( Exception exception ) { 16 System.err.println( exception.getMessage() + “n” ); 17 exception.printStackTrace(); 18 } 19 } Fig. 14.11 Using getMessageand printStackTrace(part 1 of 2).
You want to have a cheap webhost for your apache application, then check apache web hosting services.

830 Exception Handling Chapter 14 Testing and Debugging (Tomcat web server)

Friday, August 24th, 2007

830 Exception Handling Chapter 14 Testing and Debugging Tip 14.11 Avoid placing code that can throw an exception in a finally block. If such code is required, enclose the code in a try/catch within the finally block. Good Programming Practice 14.7 Java s exception-handling mechanism is intended to remove error-processing code from the main line of a program s code to improve program clarity. Do not place try/catch/finally around every statement that may throw an exception. This makes programs difficult to read. Rather, place one try block around a significant portion of your code, follow that try block with catch blocks that handle each possible exception and follow the catch blocks with a single finally block (if one is required). Performance Tip 14.3 As a rule, resources should be released as soon as it is apparent that they are no longer needed. This makes these resources immediately available for reuse and can improve program performance. Software Engineering Observation 14.15 If a try block has a corresponding finally block, the finally block will execute even if the try block exits with return, break or continue; then the effect of the return, break or continue will occur. 14.14 Using printStackTrace and getMessage Exceptions derive from class Throwable. Class Throwable offers a printStack- Trace method that prints the method call stack. By calling this method for a Throwable object that has been caught, a program can print the method call stack. Often, this is helpful in testing and debugging. Two other overloaded versions of printStackTrace enable the program to direct the stack trace to a PrintStreamor PrintWriter stream. In this section, we consider an example that exercises the printStackTrace method and another useful method, getMessage. Testing and Debugging Tip 14.12 All Throwable objects contain a printStackTrace method that prints a stack trace for the object. Testing and Debugging Tip 14.13 An exception that is not caught eventually causes Java s default exception handler to run. This displays the name of the exception, the optional character string that was supplied when the exception was constructed and a complete execution stack trace. The stack trace shows the complete method call stack. This lets the programmer see the path of execution that led to the exception file-by-file (and thus class-by-class) and method-by-method. This information is helpful in debugging a program. There are two constructors for class Throwable. The first constructor public Throwable() takes no arguments. The second constructor public Throwable( String informationString )
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Chapter 14 Exception Handling 829 (Domain and web hosting) 28 29 //

Thursday, August 23rd, 2007

Chapter 14 Exception Handling 829 28 29 // catch is incorrect type, so Exception not caught 30 catch( RuntimeException runtimeException ) { 31 System.err.println( 32 “Exception handled in method throwException” ); 33 } 34 35 // finally block always executes 36 finally { 37 System.err.println( “Finally is always executed” ); 38 } 39 } 40 41 } // end class UsingExceptions Method throwException Finally is always executed Exception handled in main Fig. 14.10 Fig. 14.10 Demonstration of stack unwinding (part 2 of 2). When method main executes, line 10 in the try block calls throwException (lines 21 39). In the try block of method throwException, line 26 throws an Exception. This terminates the try block immediately and control proceeds to the catchhandler at line 30. The type being caught (RuntimeException) is not an exact match with the thrown type (Exception) and is not a superclass of the thrown type, so the exception is not caught in method throwException. The exception must be handled before normal program execution can continue. Therefore, method throwException terminates (but not until its finallyblock executes) and returns control to the point from which it was called in the program (line 10). Line 10 is in the enclosing try block. If the exception has not yet been handled, the try block terminates and an attempt is made to catch the exception at line 14. The type being caught (Exception) matches the thrown type. Therefore, the catchhandler processes the exception and the program terminates at the end of main. As we have seen, a finally block executes for a variety of reasons, such as a try completing successfully, handling of an exception in a local catch, an exception being thrown for which no local catchis available or execution of a program control statement like a return, break or continue. Normally, the finally block executes, then behaves appropriately (we will call this finally s continuation action ) depending on the reason program control entered the block. For example, if an exception is thrown in the finally block, the continuation action will be for that exception to be processed in the next enclosing try block. Unfortunately, if there was an exception that had not yet been caught, that exception is lost and the more recent exception is processed. This is dangerous. Common Programming Error 14.13 If an exception is thrown for which no local catch is available, when control enters the local finally block, the finally block could also throw an exception. If this happens, the first exception will be lost.
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

828 Exception Handling Chapter 14 72 73 System.out.println( (My space web page)

Thursday, August 23rd, 2007

828 Exception Handling Chapter 14 72 73 System.out.println( 74 “End of method doesNotThrowException” ); 75 } 76 77 } // end class UsingExceptions Method throwException Exception handled in method throwException Finally executed in throwException Exception handled in main Method doesNotThrowException Finally executed in doesNotThrowException End of method doesNotThrowException Fig. 14.9 Demonstration of the try-catch-finallyexception-handling mechanism (part 3 of 3). The Java application in Fig. 14.10 demonstrates that when an exception thrown in a tryblock is not caught in a corresponding catchblock, the exception will be detected in the next outer try block and handled by an appropriate catch block (if one is present) associated with that outer tryblock. 1 // Fig. 14.10: UsingExceptions.java 2 // Demonstration of stack unwinding. 3 public class UsingExceptions { 4 5 // execute application 6 public static void main( String args[] ) 7 { 8 // call throwException to demonstrate stack unwinding 9 try { 10 throwException(); 11 } 12 13 // catch exception thrown in throwException 14 catch ( Exception exception ) { 15 System.err.println( “Exception handled in main” ); 16 } 17 } 18 19 // throwException throws an exception that is not caught in 20 // the body of this method 21 public static void throwException() throws Exception 22 { 23 // throw an exception and catch it in main 24 try { 25 System.out.println( “Method throwException” ); 26 throw new Exception(); // generate exception 27 } Fig. 14.10 Fig. 14.10 Demonstration of stack unwinding (part 1 of 2).
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

Chapter 14 (Web hosting asp) Exception Handling 827 20 doesNotThrowException(); 21

Wednesday, August 22nd, 2007

Chapter 14 Exception Handling 827 20 doesNotThrowException(); 21 } 22 23 // demonstrate try/catch/finally 24 public static void throwException() throws Exception 25 { 26 // throw an exception and immediately catch it 27 try { 28 System.out.println( “Method throwException” ); 29 throw new Exception(); // generate exception 30 } 31 32 // catch exception thrown in try block 33 catch ( Exception exception ) 34 { 35 System.err.println( 36 “Exception handled in method throwException” ); 37 throw exception; // rethrow for further processing 38 39 // any code here would not be reached 40 } 41 42 // this block executes regardless of what occurs in 43 // try/catch 44 finally { 45 System.err.println( 46 “Finally executed in throwException” ); 47 } 48 49 // any code here would not be reached 50 } 51 52 // demonstrate finally when no exception occurs 53 public static void doesNotThrowException() 54 { 55 // try block does not throw an exception 56 try { 57 System.out.println( “Method doesNotThrowException” ); 58 } 59 60 // catch does not execute, because no exception thrown 61 catch( Exception exception ) 62 { 63 System.err.println( exception.toString() ); 64 } 65 66 // this block executes regardless of what occurs in 67 // try/catch 68 finally { 69 System.err.println( 70 “Finally executed in doesNotThrowException” ); 71 } Fig. 14.9 Demonstration of the try-catch-finallyexception-handling mechanism (part 2 of 3).
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.