Topic 8.4: Handling Exceptions - Create and invoke a method that throws an exception Flashcards

1
Q

What is the purpose of the throw statement?

A

This statement is used to indicate exceptional conditions or errors during program execution by explicitly throwing an exception.

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

This statement is used to indicate exceptional conditions or errors during program execution by explicitly throwing an exception.

A

What is the purpose of the throw statement?

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

Give an example of a method that throws an exception explicitly.

A

Example:

public void validateInput(String input) 
{
    if (input == null) {
        throw new IllegalArgumentException("Input cannot be null");
    }
    // Perform other validation logic
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Syntax:

accessModifier returnType methodName(parameters) throws ExceptionType1, ExceptionType2, ...

A

What is the syntax for a method signature with exception throwing?

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

What is the syntax for throwing an exception?

A

Syntax:

throw exceptionInstance;

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

This is achieved by including the ‘throws’ keyword followed by the exception type(s) in the method declaration.

A

How do you indicate the possibility of throwing exceptions in a method signature in Java?

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

What is the syntax for catching and handling exceptions using catch blocks?

A

Syntax:

try {
    // Code that may throw an exception
} catch (ExceptionType1 e1) {
    // Handle exception of type ExceptionType1
} catch (ExceptionType2 e2) {
    // Handle exception of type ExceptionType2
} catch (ExceptionType3 e3) {
    // Handle exception of type ExceptionType3
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Should catch blocks for more specific exception types be placed before catch blocks for more general exception types? Why?

A

Yes, catch blocks for more specific exception types should be placed before catch blocks for more general exception types. This ensures that specific exceptions are handled appropriately before more general catch blocks are reached.

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

What can happen if catch blocks for more specific exception types are placed after catch blocks for more general exception types?

A

If catch blocks for more specific exception types are placed after catch blocks for more general exception types, the specific catch blocks will never be reached, and the more general catch blocks will handle the exceptions, resulting in incorrect or undesirable handling of specific exceptions.

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

Why is the order of catch blocks important?

A

The order of catch blocks is important because the first catch block that matches the exception type is executed.

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

How do you create and throw exceptions in Java?

A

This is achieved by using the ‘throw’ keyword followed by an instance of an exception class.

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

Syntax:

try {
    // Code that may throw an exception
} catch (ExceptionType1 e1) {
    // Handle exception of type ExceptionType1
} catch (ExceptionType2 e2) {
    // Handle exception of type ExceptionType2
} catch (ExceptionType3 e3) {
    // Handle exception of type ExceptionType3
}
A

What is the syntax for catching and handling exceptions using catch blocks?

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

Example of this situation:

public void readFile(String fileName) throws FileNotFoundException

A

Give an example of a method that can potentially throw a FileNotFoundException.

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

Syntax:

throw exceptionInstance;

A

What is the syntax for throwing an exception?

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

This is achieved by using the ‘throw’ keyword followed by an instance of an exception class.

A

How do you create and throw exceptions in Java?

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

Example:

public void divideNumbers(String dividend, String divisor) 
{
    try {
        int result = Integer.parseInt(dividend) / Integer.parseInt(divisor);
        System.out.println("Result: " + result);
    } catch (ArithmeticException e) {
        System.err.println("Error: Division by zero");
    } catch (NumberFormatException e) {
        System.err.println("Error: Invalid number format");
    }
}
A

Give an example of a method that catches and handles the ArithmeticException and NumberFormatException separately.

17
Q

Do unchecked exceptions require explicit declaration in the method signature or explicit handling?

A

No, unchecked exceptions, such as RuntimeException and its subclasses, do not need to be declared in the method signature or explicitly handled.

18
Q

These are exceptions that must be declared using the ‘throws’ clause or handled explicitly within the method.

A

What are checked exceptions in Java?

19
Q

What are checked exceptions in Java?

A

These are exceptions that must be declared using the ‘throws’ clause or handled explicitly within the method.

20
Q

In this situation the caller is responsible for either handling the exception using try-catch blocks or declaring it in its own method signature using the ‘throws’ keyword.

A

What is the responsibility of the caller when invoking a method that throws a checked exception?

21
Q

How do you indicate the possibility of throwing exceptions in a method signature in Java?

A

This is achieved by including the ‘throws’ keyword followed by the exception type(s) in the method declaration.

22
Q

Example:

public void validateInput(String input) 
{
    if (input == null) {
        throw new IllegalArgumentException("Input cannot be null");
    }
    // Perform other validation logic
}
A

Give an example of a method that throws an exception explicitly.

23
Q

Give an example of a method that can potentially throw a FileNotFoundException.

A

Example of this situation:

public void readFile(String fileName) throws FileNotFoundException

24
Q

What is the purpose of a catch block?

A

This is used to handle and respond to specific types of exceptions that occur within a try block.

25
Q

What is the order in which catch blocks are evaluated when multiple catch blocks are present?

A

Catch blocks are evaluated sequentially from top to bottom.

26
Q

What is the responsibility of the caller when invoking a method that throws a checked exception?

A

In this situation the caller is responsible for either handling the exception using try-catch blocks or declaring it in its own method signature using the ‘throws’ keyword.

27
Q

Give an example of a method that catches and handles the ArithmeticException and NumberFormatException separately.

A

Example:

public void divideNumbers(String dividend, String divisor) 
{
    try {
        int result = Integer.parseInt(dividend) / Integer.parseInt(divisor);
        System.out.println("Result: " + result);
    } catch (ArithmeticException e) {
        System.err.println("Error: Division by zero");
    } catch (NumberFormatException e) {
        System.err.println("Error: Invalid number format");
    }
}
28
Q

This is used to handle and respond to specific types of exceptions that occur within a try block.

A

What is the purpose of a catch block?

29
Q

What is the syntax for a method signature with exception throwing?

A

Syntax:

accessModifier returnType methodName(parameters) throws ExceptionType1, ExceptionType2, ...