Programming Final: Exceptions

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

What is a checked exception?

A checked exception must either be caught in a catch block or declared in a throws clause. Such exceptions often indicate serious problems that likely should lead to program termination. The exceptions BadStringOperationException, ClassNotFoundException, IOException, and NoSuchMethodException— mentioned earlier in this chapter—are all checked exceptions in the Java Class Library.

Can you catch and throw multiple exceptions?

A try block can potentially throw any number of exceptions, which can catch block can catch exceptions of only one type, but you can catch exceptions of different types by placing more than one catch block after a try block.

All java exceptions are either what or what?

All Java exceptions are categorized as either checked or unchecked.

What is an exception in java?

An exception is an object that signals the occurrence of an unusual event during the execution of a program. REMEMBER IT'S AN OBJECT

What is an unchecked exception?

An unchecked, or run-time, exception need not be caught in a catch block or declared in a throws clause. These exceptions usually indicate that something is wrong with your code and that you should fix it. Normally, you would not have written a throw statement for these exceptions. They are usually thrown during the evaluation of an expression or by a method in predefined classes. For example, if your program attempts to use an array index that is either too small or too large, an ArrayIndexOutOfBoundsException occurs. If an arithmetic operation causes a problem, such as a division by zero, an ArithmeticException occurs. For such exceptions, you should repair your code, not add a catch terminates program execution.

When should you define an exception class?

As a general rule, if you are going to insert a throw statement in your code, it is probably best to define your own exception class. That way, when your code catches an exception, your catch blocks can tell the difference between your exceptions and exceptions thrown by methods in predefined classes.

What are Errors?

Errors are objects of the class Error that are generated when certain abnormal conditions occur. Most programs should not catch them or declare them in a throws clause. Errors are technically not exceptions, even though they look like them.

What are the kinds the exceptions?

Every exception class is a descendant class of the class Exception. RuntimeException is derived from Exception, and classes derived from RuntimeException or any of its descendants represent unchecked exceptions. Such exceptions do not need to be caught or declared in a throws clause of a method's heading. All other exceptions are checked and must be either caught or declared in a throws clause.

What happens if a method throws an exception and does not catch it?

If a method throws an exception, and the exception is not caught inside the method, the method invocation ends immediately after the exception is thrown.

What happens when the thrown statement executes?

If this throw statement executes, it creates a new object of the predefined class Exception with the expression. and throws the exception object. Thestring"Exception: No milk!"is an argument for the constructor of the class Exception. The Exception object created here stores this string in an instance variable of the object so that it can be recovered later. When an exception is thrown, the code in the surrounding block stops execution, and another portion of code, known as a catch block, begins

Whats a good tip in relation to importing exceptions exceptions?

Most of the exceptions you will see in this book do not need to be imported, as they are in the package java.lang. Some, however, are in different packages and do need to be imported. For example, the class IOException is in the package java.util. When you examine the documentation for an exception, note which package contains it so you can provide an import statement if necessary.

Is there any situation where the Finally block is not thrown?

No: try block runs to the end, and no exception is thrown. In this situation, the finally block executes after the try block. try block and is caught in a corresponding catch block positioned after the try block. In this case, the finally block executes after the catch block executes. try block, but no matching catch block exists to catch the exception. The method invocation ends, and the exception object is thrown to the enclosing method. In this case, the finally block executes before the method ends. Note that you cannot account for this third case simply by placing code after the string of catch blocks.

Declaring an exception

Sometimes you don't want/need to catch a thrown exception. DivideByZeroException but that does not catch the exception would have a heading like the following one: public void sampleMethod() throws DivideByZeroException The part throwsDivideByZeroException is a throws clause. It states that an invocation of the method sampleMethod might throw a DivideByZeroException. Most exceptions that might be thrown when a method is invoked must be accounted for in one of two ways: catch block within the method definition. Declare the possible exception by writing a throws clause in the heading and let whoever uses the method worry about how to handle the exception. In any one method, you can mix these two alternatives, catching some exceptions and declaring others in a throws clause.

How does the constructor of a separate exception work?

The call to super constructor handles the message correctly, so will a class defined in this way. You should also include a default constructor in each exception class. This default constructor should set up a default value to be retrieved by getMessage super, as illustrated by the following constructor:

Catch block parameters what do they do and how are they used?

The identifier e looks like a parameter and acts very much like a parameter. So even though the catch block is not a method, we call this e the catch-block parameter. The catch is caught, so that we can write code within the catch block to manipulate the exception object. The most common name for a catch e, but you can use any legal identifier. When an exception object is thrown, it is plugged in for the catch parameter e, and the code in the catch block is executed. So in this case, you can think of e as the name of the exception object that was thrown. Every Exception object has a method called getMessage, and unless you provide code specifying otherwise, this method retrieves the string that was given to the exception object by its constructor when the exception was thrown.

Throw vs throws

The keyword throw is used to throw an exception, whereas throws is used in a method's heading to declare an exception. Thus, a throw statement throws an exception, but a throws clause declares one.

What is throwing and exception

The throw statement throws an exception. SYNTAX throw new Exception_Class_Name(Possibly_Some_Arguments); A throw statement is usually embedded in an if statement or an if-else statement. A try block can contain any number of explicit statements or any number of method invocations that might throw exceptions. EXAMPLE throw new Exception("Unexpected End of Input.");

What is the process of creating an exception called?

Throwing an exception

What is it called when you execute the catch block?

When an exception is thrown, the code in the surrounding block stops execution, and another portion of code, known as a catch block, begins catch block is called catching the exception. When an exception is thrown, it should ultimately be caught by some catch block. In Listing 9.2, the catch block immediately follows the try block. The catch block looks a little like a method definition that has a parameter. Although it is not a method definition, a catch block behaves like a method in some ways. It is a separate piece of code that is executed when a program executes a throw statement from within the preceding try block. This throw statement is similar to a method call, but instead of calling a method, it calls the catch block and causes the code in the catch block to be executed.

Whats a good tip in regards to catching multiple exceptions?

When catching multiple exceptions, the order of the catch blocks can be important. When an exception is thrown in a try block, the catch blocks are examined in order of appearance. The first block that matches the type of the exception thrown is the one that executes. The correct ordering is to reverse the catch blocks so that the more specific exception comes before its parent exception class : catch(DivideByZeroException e) { ... } catch(Exception e) { ... }

What are predefined exception classes?

When you learn about the methods of predefined classes, you will sometimes be told that they might throw certain types of exceptions. These exceptions are of predefined exception classes within the Java Class Library. If you use one of these methods, you can put the method invocation in a try block and follow it with a catch block to catch the exception. The names of predefined exception classes are: BadStringOperationException ClassNotFoundException IOException NoSuchMethodException When you catch an exception of one of these predefined exception classes, the string returned by the getMessage method will usually provide you with enough information to identify the source of the exception

What is the Finally block?

You can add a finally block after a sequence of catch blocks. The code in the finally block is executed whether or not an exception is thrown. This block gives you an opportunity to clean up any loose ends that linger as a result of the exception.

Defining exception classes

You can define your own exception classes, but they must be derived classes of some already defined exception class. An exception class can be a derived class of any predefined exception class or of any exception class that you have already successfully defined. Our examples will be derived classes of the class Exception.

Whats a good tip in relation to catching exceptions?

You can use the class Exception in a catch block, as we did in our initial examples, but catching a more specific exception, such as IOException, is more useful.

What is the code that deals with exceptions called?

You place the code that deals with the exceptional case at another place in your program— perhaps in a separate class or method. The code that detects and deals with the exception is said to handle the exception.

What is the 'try block'

catch. The idea is that the normal situation is handled by the code following the word try. A try block contains the code for the basic algorithm when everything goes smoothly. It is called a try block because you are not 100 percent sure that all will go smoothly, but you want to give it a try. If something does go wrong, you want to throw an exception, which is a way of indicating that there is some sort of problem.

What would a constructor of an exception class look like if the exception did have a predefined message?

public MySpecialException() { super("MySpecialException thrown."); //There can be more code here, but often there is none. }

What would a constructor of an exception class look like if the exception didn't have a predefined message?

public MySpecialException(String message) { super(message); //There can be more code here, but often there is none. }

What does a programmer defined exception class look like?

public class DivideByZeroException extends Exception { public DivideByZeroException() { super("Dividing by Zero!"); } public DivideByZeroException(String message) { super(message); } }

What is the 'catch block"

the code following the word catch is used only in exceptional circumstances.

What would syntax of a try block look like?

try { Code_To_Try Possibly_Throw_An_Exception More_Code } an example of throwing an exception looks like this: throw new Exception("Exception: No milk!");

What is the syntax of the Finally block?

try { ... } Catch_Block(s) finally { Using a throws clause }

What are the two most important characteristics of exception objects

■ upcoming sections explain why this is important. ■ The message that the object carries in an instance variable of type String. This string can be recovered by calling the accessor method getMessage. The string allows your code to send a message along with an exception object so that the catch block can recover the message.


Ensembles d'études connexes

CFP Lesson 1 Introduction To Risk Management Lesson

View Set

DNA/RNA/Protein Synthesis - tinker

View Set

Unit 8 Carrying for Patients with an Immune Disorder

View Set

Peds - Ch 17: Alteration in Sensory Perception - Disorder of the Eyes or Ears

View Set

Russian Revolution Key People/Groups

View Set

Chapter 7: The Fires of Nuclear Fission

View Set