Exception handling in Java practice questions
Exception handling in Java practice questions
Here are some exception handling in Java practice questions with answers:
What is the purpose of try-catch block in Java?Answer: The try-catch block in Java is used to catch and handle exceptions that occur during the execution of a program.
How do you handle an exception in Java?Answer: You can handle an exception in Java using a try-catch block. The try block contains the code that might throw an exception, the catch block contains the code that handles the exception.
What is the difference between checked and unchecked exceptions in Java?Answer: Checked exceptions are those that must be explicitly handled by the programmer, while unchecked exceptions are those that may be thrown but do not need to be explicitly handled.
How do you throw an exception in Java?Answer: You can throw an exception in Java using the throw
keyword followed by an instance of a class that extends the Exception
or RuntimeException
.
Answer: The finally block in Java is used to execute code that must be executed whether an exception occurs or not.
How do you handle a specific exception in Java?Answer: You can handle a specific exception in Java by catching that exception using the catch
keyword followed by the type of the exception.
try-with-resources
statement in Java?
Answer: The try-with-resources
statement in Java is used to automatically close resources such as files or database connections.
Answer: You can handle a nested try-catch block in Java by nesting try-catch blocks, each catching different exceptions.
What is the purpose ofthrows
clause in Java method declaration?
Answer: The throws
clause in Java is used to declare that a method may throw certain types of exceptions.
Answer: You can handle an exception in a finally block in Java by catching and handling the exception using the catch
keyword.
Example:
public class Main {
public static void main(String[] args) {
try {
int x = 5 / 0; // Divide by zero will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Caught an exception");
} finally {
System.out.println("This code is executed regardless of whether the exception was thrown or not");
}
}
}
Output:
java.lang.ArithmeticException: / by zero
at Main.main(Main.java:4)
Caught an exception
This code is executed regardless of whether the exception was thrown or not
How to output exception in Java?
I cannot provide a response in English only. However, I can help you with that.
Java provides several ways to handle and output exceptions:
try-catch block: The most common method is to use a try-catch block. This structure allows you to specify code that might throw an exception (the "try" block) followed by a catch clause that catches the exception. try {
// Code that might throw an exception
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
try-catch-finally block: A finally block is used to ensure resources are released even if an exception was thrown.
try {
// Code that might throw an exception
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
// Always executed, regardless of whether an exception is thrown or not.
}
Printing exceptions with printStackTrace()
: You can use the printStackTrace()
method to print the entire stack trace of an exception.
try {
// Code that might throw an exception
} catch (Exception e) {
e.printStackTrace();
}
Handling specific exceptions: If you want to handle a specific type of exception, you can use instanceof
operator and cast
it to the desired type.
try {
// Code that might throw an IOException
} catch (IOException e) {
System.out.println("An I/O error occurred: " + e.getMessage());
}
Logging exceptions: If you want to log your exceptions using a logging framework like Log4j or Logback, you can use their APIs.
try {
// Code that might throw an exception
} catch (Exception e) {
Logger.getLogger("MyLogger").log(Level.SEVERE, "An error occurred", e);
}
Remember to always handle exceptions in a way that is consistent with the requirements and constraints of your application.
Hope this helps!