DEV Community

Deepikandas
Deepikandas

Posted on

#32 Known is a drop! Exceptional Handling in JAVA Summary

🔹 1. What is exception handling in Java?

Exception handling is a mechanism in Java used to handle runtime errors so that the normal flow of the program is not disrupted.

🔹 2. What is an exception?

An exception is an unexpected event that occurs during program execution and disrupts normal flow.

🔹 3. Difference between Error and Exception?

Error
Serious system-level issue
Cannot be handled
Example: OutOfMemoryError, StackOverflowError
Exception
Can be handled using try-catch
Example: IOException, NullPointerException
🔹** 4. Explain exception hierarchy in Java?**

Throwable
├── Error
└── Exception
├── Checked Exception
└── Unchecked Exception (RuntimeException)
🔹 5. What are checked exceptions?

Checked exceptions are compile-time exceptions that must be handled using try-catch or declared using throws.

Examples:

IOException
FileNotFoundException
SQLException
🔹 6. What are unchecked exceptions?

Unchecked exceptions occur at runtime and are not forced by the compiler.

Examples:

ArithmeticException
NullPointerException
ArrayIndexOutOfBoundsException
NumberFormatException
🔹 7. Difference between checked and unchecked exceptions?

🔹** 8. What is try-catch block?**

try contains risky code
catch handles exceptions
🔹 9. What is finally block?

The finally block always executes whether an exception occurs or not. It is used for cleanup operations.

🔹 10. Difference between throw and throws?

🔹 11. What is throw keyword?

Used to manually create an exception.

throw new ArithmeticException("Error");
🔹 12. What is throws keyword?

Used to declare exceptions in method signature and delegate handling to caller.

void read() throws IOException
🔹 13. Why do we use throws?

To inform the caller that a method may generate a checked exception.

🔹 14. Can we handle exceptions without try-catch?

Yes, using throws, but it delegates responsibility to the caller and may cause runtime crash if not handled.

🔹** 15. What happens if exception is not handled?**

Program terminates abnormally and JVM prints stack trace.

🔹 16. What is stack trace?

A stack trace shows the sequence of method calls that led to an exception, helping in debugging.

🔹 17. Can we have multiple catch blocks?

Yes, but each catch must handle a different exception type.

🔹** 18. Can finally block be skipped?**

Generally no — it always executes, except in cases like:

System.exit()
JVM crash
🔹 19. Can we throw multiple exceptions using throw?
No, throw can throw only one exception at a time.

🔹 20. Real-world use of exception handling?

Used in:

File handling
Database operations
API calls
Input validation
Network communication
⭐ BONUS INTERVIEW ONE-LINERS

🔥 Checked = compiler forces handling
🔥 Unchecked = programming mistake
🔥 throw = create exception
🔥 throws = declare exception
🔥 finally = always executes
🔥 try-catch = handles runtime failure

Top comments (0)