8
Chapter 12 Exception Handling and Text IO 1. The advantages of using exception handling: It enables a method to throw an exception to its caller. The caller can handle this exception. Without this capability, the called method itself must handle the exception or terminate the program. Often the called method does not know 2. System.out.println( 1 / 0); // Throws an exception System.out.println( 1.0 / 0); // Will not throw an exception 3. Adding 1 to Long.MAX_VALUE exceeds the maximum value allowed by a long value. But the current versions of Java does not report this as an exception. 4. When an excepion occurs, Java searches for a handler in the catch clause. So to catch an exception in your program, you need to write a try-catch statement like this: try { } catch (Exception ex) { // Catch and process exception } 5. The printout is value is too small Continue after the catch block The printout would be if Line int value = 30; is changed to int value = 50; Continue after the catch block

12 Review

Embed Size (px)

DESCRIPTION

review

Citation preview

Chapter 12 Exception Handling and Text IO1. The advantages of using exception handling: It enables a method to throw an exception to its caller. The caller can handle this exception. Without this capability, the called method itself must handle the exception or terminate the program. Often the called method does not know.!ystem.out.println"1 # $%& ## Throws an exception !ystem.out.println"1.$ # $%& ## Will not throw an exception'.(dding 1 to )ong.*(+,-()./ exceeds the maximum 0alue allowed by a long 0alue. 1ut the current 0ersions of 2a0a does not report this as an exception.3. When an excepion occurs, 2a0a searches for a handler in the catch clause. !o to catch an exception in your program, you need to write a try4catch statement like this:try 56catch "/xception ex% 5## 7atch and process exception68. The printout is 0alue is too small7ontinue after the catch blockThe printout would be if )ine int 0alue 9 '$& is changed to int 0alue 9 8$&7ontinue after the catch block:.a. $ 1b. $;. !ee the section .a. (rithmetic/xceptionb. (rrayIndexOutOf1ounds/xceptionc. !tringIndexOutOf1ounds/xceptiond. 7lass7ast/xceptione. ?ull@ointer/xceptionf. ?o exceptionA. The purpose of declaring exceptions is to tell the 2a0a runtime system what can go wrong. Bou declare an exception using the throws keyword in the method declaration. Bou can declare multiple exceptions, separated by commas.1$. ( checked exception must be explicitly declared in the method declaration, if a method throws it. ( checked exception must be caught in a try4catch block. (n unchecked exception does not need to bedeclared and does not need to be caught. In 2a0a, the only unchecked exceptions are =untime/xception and /rror and their subclasses.11. Bou use the throw statement in the method to throw an exception. Bou cannot throw multiple exceptions in a single throw statement.1. throw is for throwing exceptions and throws is for claiming exceptions.1'. Will statement' be executedCAnswer: ?o.If the exception is not caught, will statement3 be executedCAnswer: ?o.If the exception is caught in the catch clause, will statement3 be executedCAnswer: Bes.13. =untime/xception=eason: listD1$E throws (rrayIndexOutOf1ounds/xception that is a subclass of =untime/xception.18. (rithmetic/xception=eason: method"% throws (rithmetic/xception.1:. =untime/xception in method"%(fter the method call =eason: s.char(t"'% throws !tringIndexOutOf1ounds/xception that is a subclass of =untime/xception. This exception is caught in method"%. The main method continues its normal execution flow.1;. the get*essage"% is defined in the Throwable class to return a string that describes the exception. 1>. To print trace information to the console.1A. ?o.$.The method throws a checked exception. It must be caught or thrown. Bou may fix it as follows:public 0oid m"int 0alue% throws /xception 5if "0alue F 3$%throw new /xception", and line is empty.'>. 7reate a .=) obGect and use new !canner"url.open!tream"%% to create a scanner obGect for reading datafrom the .=) stream.'A. Bes. @ossible. !uppose link1 is not in listOfTra0ersed.=)s, but it appears more than one time in a page. Huplicate link1 will be added into listOf@ending.=)s.