Exception handling Flashcards

1
Q

what is an exception?

A

//Event that disrupts normal flow of execution …

An exception in Java is an event that disrupts the normal flow of execution due to an error. It can be handled using try, catch, and optionally finally blocks to prevent program crashes and manage errors.

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

What is exception handling in java?

A

//Mechanism to maintain normal flow of execution by handling runtime exceptions it uses try catch and finally block.

Exception handling in Java is a mechanism that allows the program to manage runtime errors, so the normal flow of the application can be maintained. It uses try, catch, and finally blocks to catch exceptions and provide appropriate responses without crashing the program.

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

Give example of how would you handle :
Arithmetic Exception
Null Pointer Exception
Custom Exception

=========
FileNotFoundException?
IOException?

A

class ExampleArithmeticException {
public static void main(String[] args) {
try {
int result = 10 / 0; // Division by zero will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println(“An ArithmeticException occurred: “ + e.getMessage());
} finally {
System.out.println(“Division operation completed.”);
}
}
}
================
class ExampleNullPointerException {
public static void main(String[] args) {
String str = null;

    try {
        int length = str.length();  // Attempting to call a method on a null object
    } catch (NullPointerException e) {
        System.out.println("A NullPointerException occurred: " + e.getMessage());
    } finally {
        System.out.println("Null pointer check completed.");
    }
} } ================

class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}

class ExampleCustomException {
public static void main(String[] args) {
try {
int age = -5;
if (age < 0) {
throw new InvalidAgeException(“Age cannot be negative”);
}
} catch (InvalidAgeException e) {
System.out.println(“Custom exception occurred: “ + e.getMessage());
} finally {
System.out.println(“Age validation completed.”);
}
}
}

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

What is Check vs. Unchecked exception ?

A

Checked exceptions (like IOException, SQLException) are mandatory to handle. If you don’t handle them with a try-catch block or declare them using throws, the compiler will throw an error. These represent situations that can reasonably occur during the program’s execution (like a file not being found or network failure).

Unchecked exceptions (like NullPointerException, ArithmeticException) are not mandatory to handle. These usually result from programming mistakes, such as accessing a null object or dividing by zero, and the compiler doesn’t enforce handling them. However, if not handled, they will cause the program to crash at runtime.

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