quiz image

Exceptions

LuxuryAbundance avatar
LuxuryAbundance
·
·
Download

Start Quiz

Study Flashcards

40 Questions

What type of exception is an ArrayIndexOutOfBoundsException?

Unchecked Exception

Which of the following exceptions is a checked exception?

IOException

What is the purpose of the finally block in a try-catch block?

To release resources

What is the Throwable class in Java?

A superclass of Exception and Error

What happens to the local variables in a try block after an exception is thrown?

They are lost

What is the difference between a checked and an unchecked exception?

Checked exceptions must be handled, unchecked exceptions do not

What type of exception is a NullPointerException?

Unchecked Exception

What is the purpose of the catch block in a try-catch block?

To handle exceptions

What is the superclass of all errors and exceptions in Java?

Throwable

What is the purpose of the Finally block in a Try-Catch-Finally statement?

To perform cleanup operations

What is the difference between checked and unchecked exceptions?

Checked exceptions are subclasses of Exception, while unchecked exceptions are subclasses of RuntimeException

What is the benefit of using a single Scanner object in a project?

It reduces the risk of resource leaks

What is the purpose of the Try block in a Try-Catch-Finally statement?

To execute code that may throw an exception

What is an example of an exception that might occur in a program?

File doesn't exist

What is the benefit of using exception handling in a program?

It reduces the risk of crashes

What is the effect of using the final modifier with a class?

It prevents the class from being subclassed

What happens to the control after the last catch block is executed?

Control resumes after the last catch block.

What is the main difference between a try block and a try statement?

A try block is part of a try statement, which includes catch and/or finally blocks.

What is the purpose of the finally block in a try-catch-finally statement?

To release resources, regardless of whether an exception is thrown or not.

What is the difference between the throw and throws keywords?

Throw is used to throw an exception, while throws is used to declare an exception.

What is the characteristic of a checked exception?

It is typically caused by conditions that are not under the control of the program.

What is the best practice for catching exceptions?

Catch exceptions as close to the problem as possible.

What is the purpose of the try-with-resources statement?

To ensure that each resource is closed at the end of the statement.

What is an example of an unchecked exception?

ArithmeticException

What is the primary goal of professional programmers when it comes to exception handling?

To write bug-free programs that can gracefully handle unusual situations

What type of exception should a programmer typically not catch?

Error

What is the purpose of using a TRY…CATCH…FINALLY block?

To handle all types of exceptions and errors

What is the relationship between the Throwable class and the Exception and Error classes?

Exception and Error are subclasses of Throwable

Why is it important to handle exceptions in a program?

To make the program more robust and able to recover from errors

What happens when an exception is thrown in a TRY block?

The program executes the CATCH block

What is the main difference between a checked and an unchecked exception?

Checked exceptions are checked by the compiler, while unchecked exceptions are not

What is the correct order of execution in a TRY…CATCH…FINALLY block?

TRY, CATCH, FINALLY

What is the main purpose of using the try-catch block in exception handling?

To handle and recover from exceptions

Which of the following is a characteristic of a checked exception?

It is checked by the compiler at compile-time

What is the purpose of the Throwable class in Java's exception hierarchy?

To serve as the superclass of all exceptions

What happens when an exception is thrown in a try block and not caught by any catch block?

The exception is propagated up the call stack

What is the benefit of using a finally block in a try-catch block?

To ensure that resources are released

Which of the following is an example of a runtime exception?

ArrayIndexOutOfBoundsException

What is the primary difference between a checked and an unchecked exception?

Checked exceptions are caught by the compiler, while unchecked exceptions are not

Why is it a good practice to handle exceptions as close to the source of the exception as possible?

To ensure that the exception is handled in the correct context

Study Notes

Exceptions

  • An exception is a strange event that can potentially make a program fail.
  • Exceptions can be handled using TRY…CATCH…FINALLY.

Exceptions Hierarchy

  • Throwable: supertype of all exceptions and errors in Java.
  • Three subclasses of Throwable:
    • Error: system errors, cannot be caught (e.g. OutOfMemoryError).
    • Exception: base class for all exceptions in Java, programmers throw and catch exceptions.
    • RuntimeException: a runtime programming error, cannot be thrown and caught (e.g. NullPointerException).

Types of Exceptions

  • Checked Exceptions: typically caused by conditions that are not under the control of the program, compiler enforces special requirements (e.g. IOException, ClassNotFoundException).
  • Unchecked Exceptions: all exception types that are direct or indirect subclasses of RuntimeException, avoidable by bulletproofing your code (e.g. ArrayIndexOutOfBoundsExceptions, ArithmeticExceptions).

Exception Handling

  • Try block: specifies the requirement syntax, where the exception can occur.
  • Catch block: handles the exception, can rethrow an exception.
  • Finally block: always executes when the try block exits, typically used to release resources.
  • Catch should be as close to the problem as possible, and place catch blocks from the specific to general.

Throws and Throw

  • Throw: used to throw an exception for a method, cannot throw multiple exceptions.
  • Throws: used to indicate what exception types may be thrown by a method, can declare multiple exceptions.

Best Practices

  • Catch should be as close to the problem as possible.
  • Place catch blocks from the specific to general.
  • A catch block can rethrow an exception.
  • Reuse existing exception classes where possible, but you can create new ones if needed.
  • Only the first matching catch executes.

Final Modifier

  • With variable: becomes constant.
  • With method: cannot be overridden in subclasses.
  • With class: cannot be sub-classed.

This quiz covers the concepts of exceptions in Java, including types of exceptions, exception hierarchy, and error handling. It is ideal for students and programmers learning Java.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser