Exception Handling Flashcards

1
Q

How do the ‘try’, ‘catch’, and ‘finally’ statements interact?

A

Example:
try {
Code to run;
Statement or method that may throw an exception;
More code to run if no exception;
}

catch (exceptionType ex) {
Code to process the exception;
}

finally {
This code always runs, whether or not an exception is thrown in the try statement.
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are exceptions?

A

They are objects created from the exception class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a checked exception?

A

A checked exception is an exception that the compiler forces the programmer to deal with, either with a try-catch block or declare it in the method header.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you deal with checked exceptions?

A

You can either catch it in an ordinary try statement, like this:
public static void openFile(String name)
{
try
{
FileInputStream f =
new FileInputStream(name);
}
catch (FileNotFoundException e)
{
System.out.println(“File not found.”);
}
}

or, if you don’t want to deal with the exception in the method that creates the exception, you can throw it “up the line”, back to the caller like this:
public static void openFile(String name)
throws FileNotFoundException
{
// code block
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is meant by “rethrowing exceptions”?

A

Java allows an exception handler to rethrow the exception if the handler cannot process the exception or simply wants to let its caller be notified of the exception. The syntax looks like this:
try {
statements;
}

catch (TheException ex) {
perform operations before exits;
throw new ex;
}

The statement “throw new ex;” rethrows the exception back to the caller so that other handlers in the caller get a chance to process the exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When should you create your own exception class?

A

You should use the built-in exception classes whenever possible, but if you run into a problem that cannot be adequately described by the exception classes, you can create your own exception class, derived from Exception or from a subclass of Exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

Errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

An instance of ___ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.

A

Exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

An instance of ___ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

A

RuntimeException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
int[] list = new int[5];
System.out.println(list[5]);
}
}

A

ArrayIndexOutOfBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

All are true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is wrong in the following program?

class Test {
public static void main (String[] args) {
try {
System.out.println(“Welcome to Java”);
}
}
}

A

You cannot have a try block without a catch block or a finally block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does the throws keyword (throw clause) do when placed after the method parameters closing parenthesis and before the opening curly brace for the body

A

it allows a calling method to actually deal with the exception later, and not to deal with it directly in the method where the exception occurred.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

can a throws clause have multiple exceptions?

A

yes, multiple comma separated exceptions can be included in a throws clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does throws Exception do?

A

It is the same as declaring that any class that extends from the Exception class can be thrown. that way you don’t have to list them all out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

NullPointerException - Checked or Unchecked?

A

Unchecked - Do not have to manage

17
Q

ArrayIndexOutOfBouundsException - Checked or Unchecked?

A

Unchecked - Do not have to manage

18
Q

FileNotFoundException - Checked or Unchecked?

A

Checked - Must manage

19
Q

what order do multiple catch blocks need to be in?

A

The order of the catch blocks needs to go from most specific to least specific. This is because the first exception that matches will be the code block executed

20
Q

can a single catch block catch different types of exceptions?

A

Yes, they should be separated by the pipe (|) character.

21
Q

how can we throw our own exceptions?

A

using throw keyword followed by the keyword new and the name of the exception class with an optional message inside curved brackets and quotation marks.
ex: throw new MiddleIntialException(“Middle initial is only one character”);

22
Q

What is the hierarchy of the Throwable class?

A

Throwable
Error
Exception
checked
unchecked

23
Q

How can we handle errors such as OutOfMemoryError & StackOverflowError ?

A

The Error class represents more severe errors that generally cannot be handled and recovered from, such as OutOfMemoryError and StackOverflowError. Unlike exceptions, errors are not intended to be caught or handled by application code.

24
Q

This exception is thrown when an input/output operation fails or is interrupted, such as when trying to read from or write to a file that does not exist.

A

IOException - Checked - import java.io.IOException

25
Q

This exception is thrown when an error occurs while working with a database, such as when executing a SQL statement that is invalid or when the database connection is lost.

A

SQLException - Checked - import java.sql.SQLException

26
Q

This is a broad category of exceptions that includes many different types of unchecked exceptions.

A

RunTimeException - Unchecked - It is part of the java.lang package, so it does not need to be explicitly imported.

27
Q

This exception is thrown when trying to load a class that does not exist at runtime, such as when trying to load a JDBC driver that is not on the classpath.

A

ClassNotFoundException - Checked - It is part of the java.lang package, so it does not need to be explicitly imported.

28
Q

This exception is thrown when a thread is interrupted while it is waiting or sleeping, such as when another thread calls the interrupt() method on it.

A

InterruptedException - Unchecked - imports with the java.lang package.

29
Q

This exception is thrown when a parsing operation fails, such as when trying to convert a string to a date using an invalid date format.

A

ParseException - Unchecked - It is part of the java.lang package, so it does not need to be explicitly imported.

30
Q

This exception is thrown when trying to parse a string that does not represent a valid number

A

NumberFormetException - Unchecked - It is part of the java.lang package, so it does not need to be explicitly imported.

31
Q

Will this code allow the program to continue running if num2 == 0
?public void divide(int num1, int num2) throws ArithmeticException {
if (num2 == 0) {
throw new ArithmeticException(“Cannot divide by zero”);
}
int result = num1 / num2;
System.out.println(“Result: “ + result);
}

A

No, the exception is thrown but not caught. The code can be changed like this to catch the exception and allow the code to continue:
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;
try {
divide(num1, num2);
} catch (ArithmeticException e) {
System.out.println(“Error: “ + e.getMessage());
}
System.out.println(“Program continues to run”);
}

32
Q

Why do we throw errors rather than just dealing with them where they happen?

A

Throwing an exception allows a method to signal an error or exceptional situation, and propagating the exception to a higher level of the program’s execution stack allows for centralized and consistent handling of these situations.

33
Q

Can we throw and catch an exception in one method?

A

Yes, it is possible to throw and catch an exception in the same method. In fact, this is a common practice in Java programming.

34
Q

what is the syntax for try, catch, finally?

A

try {
// code that might throw an exception
} catch (Exception e) {
// code to handle the exception
} finally {
// code that will always be executed
}

35
Q

Can we catch more than one exception?

A

Yes: try {
// code that might throw an exception
} catch (ExceptionType1 e) {
// code to handle ExceptionType1
} catch (ExceptionType2 e) {
// code to handle ExceptionType2
}

36
Q

what is the syntax to import the FileNotFoundException?

A

import java.io.FileNotFoundException;

37
Q

____________is a type of unchecked exception that is thrown when trying to access an array with an invalid index.

A

ArrayIndexOutOfBoundsException - unchecked - It is part of the java.lang package, so it does not need to be explicitly imported.

38
Q

____________ is an unchecked exception in Java that occurs when a program tries to access or use a reference variable that has a null value.

A

NullPointerException - unchecked - It is part of the java.lang package, so it does not need to be explicitly imported.