An exception is an abnormal situation that occurs while a program is running. It does not always mean the program must crash. With a planned handler, the program can explain the problem, record it, or continue with later logic.
Common exceptions
An exception type identifies the category of failure:
NumberFormatExceptionAn unsuitable value, such as converting "3.6" to int.
ArithmeticExceptionInteger division by zero.
ArrayIndexOutOfBoundsExceptionAn array index is outside its range.
NullPointerExceptionA member is accessed through null.
FileNotFoundExceptionA requested file is missing.
IllegalArgumentExceptionAn argument does not satisfy a method's requirements.
Exceptions should not replace ordinary conditions. Use if for simple rules; exceptions fit failures discovered only while an operation runs.
KNOWLEDGE QUIZ
try, catch, and finally
try contains operations that may fail. Once an exception occurs, remaining statements in try are skipped. Java finds a matching catch and transfers control to its handler. Statements later in try form the success path and run only if no earlier statement throws; finally performs cleanup on either path.
Exception path debugger: from risky line to safe cleanup
Choose an input and step through execution. The highlighted line, variables, exception object, and control flow stay synchronized.
01try {02 result = Integer.parseInt(a) / Integer.parseInt(b);03 message = String.valueOf(result);04 success = true;05} catch (ArithmeticException | NumberFormatException exception) {06 message = exception.getClass().getSimpleName();07} finally {08 cleaned = true;09}
1/6 · tryimport java.util.Scanner;
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter dividend: ");
int dividend = Integer.parseInt(scanner.nextLine());
System.out.print("Enter divisor: ");
int divisor = Integer.parseInt(scanner.nextLine());
int quotient = dividend / divisor;
System.out.println("Result: " + quotient);
} catch (NumberFormatException exception) {
System.out.println("Only integers are supported.");
} catch (ArithmeticException exception) {
System.out.println("Divisor cannot be 0.");
} finally {
System.out.println("This calculation is finished.");
}KNOWLEDGE QUIZ
Catch specific exceptions
Do not begin with an overly broad catch (Exception exception). Catching everything may swallow genuine bugs. Catch the failures expected from the current operation so the reason and response remain clear.
try {
int score = Integer.parseInt(text);
} catch (NumberFormatException exception) {
System.out.println("Score must be an integer.");
}KNOWLEDGE QUIZ
Throw an exception explicitly
throw lets a method report that input violates its requirements. The method detects the problem; its caller decides whether to explain, retry, or record it.
static double normalizeScore(int score) {
if (score < 0 || score > 100) {
throw new IllegalArgumentException("Score must be between 0 and 100.");
}
return score / 100.0;
}
try {
double ratio = normalizeScore(120);
} catch (IllegalArgumentException exception) {
System.out.println("Invalid input: " + exception.getMessage());
}Create a Circle object safely
- The Circle constructor throws IllegalArgumentException for a negative radius.
- The main flow converts input to float and creates the object inside try.
- Catch IllegalArgumentException from invalid text or a negative radius and print its message.
- Add finally so “Check finished” always prints.