CHAPTER 12 EXCEPTIONS HANDLING OVERVIEW

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

An instance of _________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

A

Analyze the following code: public class Test { public static void main(String[] args) throws MyException { System.out.println("Welcome to Java"); } } class MyException extends Error { } A. You should not declare a class that extends Error, because Error raises a fatal error that terminates the program. B. You cannot declare an exception in the main method. C. You declared an exception in the main method, but you did not throw it. D. The program has a compile error.

A

What exception type does the following program throw? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } } A. ArithmeticException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. ClassCastException E. No exception

A

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (NumberFormatException ex) { System.out.println("NumberFormatException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } static void method() { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } } A. The program displays NumberFormatException. B. The program displays NumberFormatException followed by After the method call. C. The program displays NumberFormatException followed by RuntimeException. D. The program has a compile error. E. The program displays RuntimeException.

A

Which of the following statements are true? A. You use the keyword throws to declare exceptions in the method heading. B. A method may declare to throw multiple exceptions. C. To throw an exception, use the key word throw. D. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.

ABCD

An instance of _________ are unchecked exceptions. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

ACE

Point out the problem in the following code. Does the code throw any exceptions? long value = Long.MAX_VALUE + 1; System.out.println (value);

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.

A method must declare to throw ________. A. unchecked exceptions B. checked exceptions C. Error D. RuntimeException

B

An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

B

How is a method handler found?

By propagating the exception backward through a chain of method calls, starting from the current method.

An instance of _________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

C

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } static void method() throws Exception { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } } A. The program displays RuntimeException twice. B. The program displays Exception twice. C. The program displays RuntimeException followed by After the method call. D. The program displays Exception followed by RuntimeException. E. The program has a compile error.

C

A Java exception is an instance of __________. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

D

Analyze the following code: public class Test { public static void main(String[] args) { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; } catch (Exception ex) { System.out.println("NumberFormatException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } } A. The program displays NumberFormatException. B. The program displays RuntimeException. C. The program displays NumberFormatException followed by RuntimeException. D. The program has a compile error.

D. sub before super

The following code causes Java to throw _________. int number = Integer.MAX_VALUE + 1; A. RuntimeException B. Exception C. Error D. Throwable E. no exceptions

E

Which exceptions do not require explicit declaration and why?

Error and RuntimeException because they are very common and can occur in any code.

True or false: The following statement will throw an exception: System.out.println(1.0 / 0); // Will not throw an exception

False

True / False: a throw statement must be used to invoke a method with a thow exception

False. The method with the exception can be thrown

What is the advantage of exception handling:

It enables a method to throw an exception to its caller, enabling the caller to handle the exception. Without this capability, the called method itself must handle the exception or terminate the program. Often the called method does not know what to do in case of error. This is typically the case for the library methods. The library method can detect the error, but only the caller knows what needs to be done when an error occurs. The key benefit of exception handling is separating the detection of an error (done in a called method) from the handling of an error (done in the calling method).

What are the three procedures that java exception modelling follows?

Java's exception-handling model is based on three operations: declaring an exception, throwing an exception, and catching an exception,

What are the reasons for the following exceptions: - VirtualMachineError - LinkageError

LinkageError: A class has some dependency on another class, but the latter class has changed incompatibly after the compilation of the former class. VirtualMachineError: The JVM is broken or has run out of the resources it needs in order to continue operating.

What are Checked and UnChecked Exceptions?

RuntimeException, Error, and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with them in a try-catch block or declare it in the method header.

Analyze the following program. public class Test { public static void main(String[] args) { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (Exception ex) { System.out.println(ex); } } } A. An exception is raised due to Integer.parseInt(s); B. An exception is raised due to 2 / i; C. The program has a compile error. D. The program compiles and runs without exceptions.

The correct answer is A Explanation: Both (A) and (B) would cause exception, but (A) occurred first, so the exception is due to (A). Order matters.

What is the purpose of declaring exceptions? How do you declare an exception, and where? Can you declare multiple exceptions in a method header?

The purpose of declaring exceptions is to tell the Java runtime system what can go wrong. You declare an exception using the throws keyword in the method declaration. You can declare multiple exceptions, separated by comma

What is the root class for exceptions?

The root class for exceptions is java.lang.Throwable .

How is a throw statement analogous to a method. How is a catch block similar to a method.

The throw statement is analogous to a method call, but instead of calling a method, it calls a catch block. In this sense, a catch block is like a method definition with a parameter that matches the type of the value being thrown. Unlike a method, however, after the catch block is executed, the program control does not return to the throw statement; instead, it executes the next statement after the catch block.

True / False: If a method does not declare exceptions in the superclass, you cannot override it to declare exceptions in the subclass.

True

True /False: Exceptions are objects

True

True or False: A compile error will result if a catch block for a superclass type appears before a catch block for a subclass type

True. A subtype must be called before a supertype

What does the JVM do when an exception occurs? How do you catch an exception?

When an exception 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 }

How do you throw an exception? Can you throw multiple exceptions in one throw statement?

You use the throw statement in the method to throw an exception. You cannot throw multiple exceptions in a single throw statement.

Show the output of the following code. (a) public class Test { public static void main(String[] args) { for (int i = 0; i < 2; i++) { System.out.print(i + " "); try { System.out.println(1 / 0); } catch (Exception ex) { } } } } (b) public class Test { public static void main(String[] args) { try { for (int i = 0; i < 2; i++) { System.out.print(i + " "); System.out.println(1 / 0); } } catch (Exception ex) { } } }

a. 0 1 b. 0

What RuntimeException will the following programs throw, if any? (a) public class Test { public static void main(String[] args) { System.out.println(1 / 0); } } (b) public class Test { public static void main(String[] args) { int[] list = new int[5]; System.out.println(list[5]); } } (c) public class Test { public static void main(String[] args) { String s = "abc"; System.out.println(s.charAt(3)); } } (d) public class Test { public static void main(String[] args) { Object o = new Object(); String d = (String)o; } } (e) public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } } (f) public class Test { public static void main(String[] args) { System.out.println(1.0 / 0); } }

a. ArithmeticException b. ArrayIndexOutOfBoundsException c. StringIndexOutOfBoundsException d. ClassCastException e. NullPointerException f. No exception

The code that handles the exception is called the ;

exception handler

Write the syntax or delcaring an IOexception in a method header

public void myMethod() throws IO exception

What are the three main types of exception classes?

system errors, exceptions, and runtime exceptions.

Which block handles an exception?

the catch block

What is the keyword to declare an exception? What is the keyword to throw an exception?

throws, throw


Ensembles d'études connexes

Test Review #5 Test CHF, Arrhythmias, PCA-Epidurals

View Set

Neurons and Immune practice questions

View Set

Chapter 18 - A History of World Societies Vocab

View Set