Exceptions Flashcards
Explain how exception handling works in java?
educative.io/courses/java-interview-handbook/B8RkJmgm8No
When an exception occurs in some method then it should be handled by the “try-catch” section. If it is not handled then in a method in which the exception occurred then it is propagated to the higher level method (a method that invoked method with exception) and it continues to propagate to a higher level method until some exception handler is found. If it is not found then the runtime system is terminated.
[1] What are the different types or kinds of exceptions?
[1] There are two categories of exceptions in Java:
a) Checked Exceptions
b) Unchecked Exceptions
[1] What are checked exceptions?
[2] Examples of checked exceptions.
[1] In general, checked exceptions represent errors outside the control of the program. For example, the constructor of FileInputStream throws FileNotFoundException if the input file does not exist.
Java verifies checked exceptions at compile-time.
Checked exception should be handled in try-catch section or declared in after throws signature.
[2] Examples:
a) IOException - Working with the file system or data streams using java.io package
Creating network applications using java.net package
- FileNotFoundException is a common type of IOException while working with the file system
- MalformedURLException
When working with URLs, we might encounter with MalformedURLException – if our URLs are invalid.
b) ParsingException np. JsonParsingException
Java uses text parsing to create an object based on a given String. If parsing causes an error, it throws a ParseException.
c) SQLException- An exception that provides information on a database access error or other errors.
[1] What are unchecked exceptions?
[2] Examples of unchecked exceptions.
[1] Java does not verify unchecked exceptions at compile-time. Furthermore, we don’t have to declare unchecked exceptions in a method with the throws keyword. And although the above code does not have any errors during compile-time, it will throw ArithmeticException at runtime.
Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.
The RuntimeException class is the superclass of all unchecked exceptions, so we can create a custom unchecked exception by extending RuntimeException:
[2]
When to Use Checked Exceptions and Unchecked Exceptions
The Oracle Java Documentation provides guidance on when to use checked exceptions and unchecked exceptions:
“If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.”
Describe exceptions class hierarchy.
class Throwable
|
Error
|
StackOverflowError
OutOfMemoryError
|
Exception
|
- IOException, SQLException, FileNotFoundException
- RuntimeException
|
NullPointerException
ArithemticException
What objects can be used with try-with-resources statement?
Objects that implement the interface java.lang.AutoCloseable can be used with the try-with-resources statement.