What Is An Exception in Jav
What Is An Exception in Jav
try
catch
finally
throw
throws
4. What is the purpose of the throw and throws keywords?
throw: Used to explicitly throw an exception in a method or block
of code.
throws: Used in a method signature to declare that the method
might throw one or more exceptions.
5. How can you handle an exception?
You can handle an exception using a try-catch block. Place the
code that might throw an exception inside the try block, and
handle the exception in the catch block. Optionally, you can use a
finally block to execute code regardless of whether an exception
was thrown or caught.
Example:
try {
// Code that might throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code that will always execute
}
6. Explain Java Exception Hierarchy?
The exception hierarchy in Java is as follows:
java.lang.Object
└── java.lang.Throwable
├── java.lang.Exception
│ ├── java.io.IOException
│ ├── java.sql.SQLException
│ └── java.lang.RuntimeException
│ ├── java.lang.NullPointerException
│ ├── java.lang.ArrayIndexOutOfBoundsException
│ └── java.lang.ArithmeticException
└── java.lang.Error
├── java.lang.OutOfMemoryError
├── java.lang.StackOverflowError
└── java.lang.VirtualMachineError
7. How can you catch multiple exceptions?
You can catch multiple exceptions by using multiple catch blocks
or a single catch block that handles multiple exception types (Java
7 and later).
Example:
try {
// Code that might throw multiple exceptions
} catch (IOException | SQLException e) {
// Code to handle IOException or SQLException
} catch (Exception e) {
// Code to handle other exceptions
}
8. What is the difference between Checked and Unchecked
Exceptions in Java?
Checked Exceptions: Checked at compile-time. Must be either
caught or declared in the method signature using the throws
keyword. Examples: IOException, SQLException.
Unchecked Exceptions: Not checked at compile-time. They are
subclasses of RuntimeException. Examples: NullPointerException,
ArrayIndexOutOfBoundsException.
9. What is the difference between the throw and throws keywords
in Java?
throw: Used to explicitly throw an exception. Example:
throw new IllegalArgumentException("Invalid argument");
throws: Used in a method signature to declare that the method
might throw one or more exceptions. Example:
public void method() throws IOException, SQLException {
// Method body
}
10. What is the difference between an exception and an error?
Exception: Represents conditions that a program might want to
catch and handle. They are recoverable conditions.
Error: Represents serious issues that a reasonable application
should not try to catch. They are usually external to the
application and indicate problems with the environment, such as
the Java Virtual Machine (JVM) running out of memory.
11. What is OutOfMemoryError in Java?
OutOfMemoryError is an error that occurs when the Java Virtual
Machine (JVM) cannot allocate an object because it is out of
memory. The JVM throws this error to indicate that the heap
memory has been exhausted.
Example:
Example:
Example:
Example:
@FunctionalInterface
interface ThrowingConsumer<T> {
void accept(T t) throws Exception;
}
try {
consumer.accept(-1);
} catch (Exception e) {
System.out.println("Caught exception: " +
e.getMessage());
}
}
}
20. What are the rules we need to follow when overriding a
method that throws an exception?
When overriding a method that throws an exception:
Example:
class Parent {
public void method() throws IOException {
// Method body
}
}