Chapter 6: exceptions Flashcards
A(n) ____ exception happens during runtime.
unchecked
What’s the difference between an exception and compiler error?
All exceptions happen at runtime. Compiler error happens before the program is run.
A runtime exception is also known as:
unchecked exception
A _____ exception inclucdes Exception and all subclassed that do not extend Runtime Exception
checked exception
Is this a checked or unchecked exception?
void fall() throws Exception{
throw new Exception();
}
Checked. Because we tell the program how to handle the exception be declaring.
What does the ‘throw’ keyword indicate when used in a program?
Just that this method might throw an exception
Is the NullPointerException checked or unchecked?
unchecked. We didn’t tell the program what to do and it happened at runtime.
How do you recognize a Runtime exception? What will it’s super class be?
RuntimeException
Does Java force you to explicit handle a runtime exception with something like a throws() or try catch?
No
How do you recognize a checked exception? What will it’s super class be?
It will be a subclass of Exception but not of RuntimeException.
Does Java force you to handle or declare a checked exception?
yes
How do you recognize an error exception? What will it’s superclass be?
Subclass of Error
Will the program catch an Error (are errors fatal?), and are you required to handle or declare an exception to handle an Error.
Errors are fatal. Errors are not declared.
What does Java use ‘try’ statements for?
To separate the logic that might throw an exception from the logic to handle that exception.
Are curly braces required in a try catch statement?
Yes.
What happens to the catch block of code if the code in the try block doesn’t fail?
It doesn’t run.
Are the curly braces in a try catch required around both the try block and the catch clause?
Yes
Why doesn’t this compile?
try
fall();
catch(Exceptione e)
System.out.print(“Get up”);
No. the try block requires curly braces
Does this compile?
try{
fall();
}
No. You must have a catch statement in a try/catch block.
When does the finally{} clause execute?
The finally clause executes no matter what in a try catch statement. If the try is successful, the finally still runs. If the try is unsuccessful and the catch clause runs, the finally block will still run.
Does finally have to follow a catch statement in a try catch block?
yes. if the catch doesn’t precede the finally the code will not compile.