InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
Revision Mode
Document technical questions and best-practice answers.
What is the exception handeling in Java and what are the major exceptions related to Java?
Exception handling in Java is a way to manage errors or unexpected situations that happen during the execution of a program. It helps prevent the program from crashing and allows you to handle problems gracefully.
How it works:
1: Java uses try, catch, and finally blocks.
2: Code that might cause an error goes inside the try block.
3: If an error (exception) occurs, it is "caught" by the catch block, where you can write code to handle the problem.
4: The finally block (optional) runs code no matter what, even if an exception happened.
Major Exceptions in Java (Easy to Remember):
1: NullPointerException – When you try to use an object that is null.
2: ArrayIndexOutOfBoundsException – When you try to access an invalid index in an array.
3: ArithmeticException – When you do illegal math, like dividing by zero.
4: ClassNotFoundException – When Java can’t find a class you are trying to use.
5: NumberFormatException – When you try to convert a string to a number, but the string isn’t a valid number.
6: FileNotFoundException – When a file you are trying to read doesn’t exist.
In summary:
Exception handling in Java helps you deal with errors without stopping the whole program. Just remember: use try-catch blocks, and know the common exceptions so you can handle them easily!