Java Exceptions: try, catch, finally, throw, throws (ISC Class 11 Computer Science)
Updated 17 October 2026 · Fact-checked
A compile-time error (syntax) is found by the compiler; a run-time error occurs while the program runs and, in Java, appears as an exception, an object that signals an abnormal event. Code that may fail is placed in a try block; a catch block handles a named exception; finally runs whether or not an exception occurs; throw raises an exception and throws declares that a method may pass one on. An uncaught exception propagates up the chain of method calls.
Notice the red dashed path: an exception not caught in div() passes up to the caller, where catch handles it and finally always runs.
Illustration drawn for QuestionBuddy.
Understand Errors and Exceptions: try, catch, finally, throw, throws and Propagation
Errors. *Compile-time:* syntax errors found by javac; the program does not run. *Run-time:* occur during execution (division by zero, array index out of range, invalid conversion, missing file). *Logical:* the program runs but gives wrong results.
Exceptions. Objects that represent abnormal events. Common ones: ArithmeticException (division by zero), ArrayIndexOutOfBoundsException, NumberFormatException (Integer.parseInt("abc")), NullPointerException, StringIndexOutOfBoundsException, IOException (checked; must be handled or declared).
try-catch-finally.
try {
int a = 10 / 0;
System.out.println("skipped");
} catch (ArithmeticException e) {
System.out.println("Err");
} finally {
System.out.println("Done");
}The statements after the point of failure in try are skipped; the matching catch runs; finally always runs (even with a return in try). Several catch blocks may follow one try; the first matching one runs.
throw and throws. throw new ArithmeticException("negative"); raises an exception explicitly. throws in a method header, void m() throws IOException, declares that the method may pass an exception to its caller.
Propagation. If a method does not catch the exception, it passes to the calling method, and so on up to main; if no one catches it the program ends with an error message.
static int div(int a, int b) { return a / b; }
// in main:
try { System.out.println(div(10, 2)); System.out.println(div(5, 0)); System.out.println("After"); }
catch (ArithmeticException e) { System.out.println("Cannot divide"); }
finally { System.out.println("End"); }Output: 5, Cannot divide, End.
Errors and Exceptions: try, catch, finally, throw, throws and Propagation: key facts
- Block order
try the risky code catch (Type e) handler for that exception finally always runs - Structure.
- Raising and declaring
- throw: raise an exception | throws: declare that a method may pass one on
- Do not confuse.
- Common exceptions
- ArithmeticException
- ArrayIndexOutOfBoundsException
- NumberFormatException
- NullPointerException
- IOException
- Causes differ.
How to answer Errors and Exceptions: try, catch, finally, throw, throws and Propagation questions
Find the first statement that throws and follow the control.
- 1Mark the statement that fails and the exception type.
- 2Skip the rest of the try block.
- 3Find the first catch that matches; run it.
- 4Run finally; then continue after the whole try-catch.
Errors and Exceptions: try, catch, finally, throw, throws and Propagation in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
finally
Suppose a program opens a file, reads it, and a read error occurs.
Why is the file closed in a finally block?
Try it first, then show the solutionSolution
- An error may stop the try block before the close statement.
- finally runs in every case, so the file is always closed.
Answer: finally guarantees clean-up even after an exception.
Memory hook: finally means no matter what.
Tricks to remember Errors and Exceptions: try, catch, finally, throw, throws and Propagation
Short memory aids. Say each one aloud once, then use it on a question the same day.
Try, catch, finally
Attempt, handle, always.
Use it when: Blocks.
throw acts, throws warns
throw raises; throws declares.
Use it when: Keywords.
Skipped after the failure
Rest of try is not executed.
Use it when: Tracing.
Errors and Exceptions: try, catch, finally, throw, throws and Propagation: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Keyword · 1 mark · MCQBoard level
The block that always executes whether an exception occurs or not is:
(a) try (b) catch (c) finally (d) throw
Try it first, then show the solutionSolution
- finally always runs.
Answer: (c) finally
Example 2: throw vs throws · 2 marks · Short answerBoard level
Distinguish between throw and throws. (2 marks)
Try it first, then show the solutionSolution
- throw is a statement that raises an exception object.
- throws is in the method header and declares exceptions the method may pass on.
Answer: throw raises an exception explicitly inside a method; throws declares in the header that the method may pass on an exception.
Example 3: Trace the output · 4 marks · Short answerHarder board level
Give the output:
try {
System.out.println(div(10, 2));
System.out.println(div(5, 0));
System.out.println("After");
} catch (ArithmeticException e) {
System.out.println("Cannot divide");
} finally {
System.out.println("End");
}where div(a, b) returns a / b. What changes if the second call is div(5, 1)? (4 marks)
Try it first, then show the solutionSolution
- div(10, 2) prints 5.
- div(5, 0) throws ArithmeticException: "After" is skipped; catch prints Cannot divide; finally prints End.
- With div(5, 1): prints 5, 5, After, End (no catch).
Answer: 5
Cannot divide
End. With div(5, 1): 5, 5, After, End.
Example 4: Several catches · 5 marks · Long answerHarder board level
Give the output for each input value of k: int a[] = {4, 0, 8}; try { int r = 8 / a[k]; System.out.println(r); } catch (ArithmeticException e) { System.out.println("A"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("B"); } finally { System.out.println("F"); } for k = 0, 1 and 3. (5 marks)
Try it first, then show the solutionSolution
- k = 0: 8 / 4 = 2 prints 2, then F.
- k = 1: 8 / 0 raises ArithmeticException: prints A, then F.
- k = 3: a[3] is out of bounds: prints B, then F.
Answer: k = 0: 2 then F; k = 1: A then F; k = 3: B then F.
Example 5: Propagation and finally with return · 5 marks · Long answerHOTS / competency
Trace: static int f() { try { return 1; } finally { System.out.println("F"); } } static void g() { int x = 5 / 0; } static void h() { try { g(); } finally { System.out.println("H"); } } In main: System.out.println(f()); try { h(); } catch (ArithmeticException e) { System.out.println("Caught in main"); } (5 marks)
Try it first, then show the solutionSolution
- f(): the try returns 1 but finally prints F before the value is returned: F then 1.
- h() calls g(), which throws ArithmeticException; h has no catch, so finally prints H and the exception propagates to main.
- main catches it: Caught in main.
Answer: F
1
H
Caught in main
Where students lose marks in Errors and Exceptions: try, catch, finally, throw, throws and Propagation
The slips examiners see most often, and what to write instead.
Mistake: Executing statements after the failure in try
Ignoring control transfer.
Wrong working
After is printed
Right working
After is skipped
Do this: Control jumps to the matching catch.
Usually costs: 2 marks.
Mistake: Writing catch for a different exception
No match.
Wrong working
catch (NumberFormatException e) for division by zero
Right working
catch (ArithmeticException e)
Do this: Match the type.
Usually costs: 1-2 marks.
Mistake: Mixing throw and throws
Similar words.
Wrong working
void m() throw IOException
Right working
void m() throws IOException
Do this: Header uses throws.
Usually costs: 1 mark.
Exam tips
- State which exception occurs and where.
- Show the order of output including finally.
- Explain propagation in one sentence.
Practice questions on Errors and Exceptions: try, catch, finally, throw, throws and Propagation
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Integer.parseInt("12a") throws:
(a) ArithmeticException (b) NumberFormatException (c) NullPointerException (d) IOExceptionHint
Wrong number format.
Answer
(b) NumberFormatException
Q2 · 3 marks · Short answerBoard level
What is an exception? Name two run-time exceptions with a cause for each. (3 marks)
Hint
Abnormal event; examples.
Answer
An exception is an object signalling an abnormal event at run time. ArithmeticException: division by zero; ArrayIndexOutOfBoundsException: index outside the array.
Q3 · 3 marks · Short answerHarder board level
Give the output: String s = null; try { System.out.println(s.length()); } catch (NullPointerException e) { System.out.println("N"); } catch (Exception e) { System.out.println("E"); } finally { System.out.println("F"); } (3 marks)
Hint
s is null.
Answer
N then F. s.length() throws NullPointerException; the first matching catch runs, then finally.
Q4 · 4 marks · Short answerHarder board level
Write a method that reads an integer from a string and returns it, returning -1 when the string is not a valid number; test with "45" and "4x5". (4 marks)
Hint
try-catch around parseInt.
Answer
static int toInt(String s) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { return -1; } } toInt("45") returns 45; toInt("4x5") returns -1.
Q5 · 3 marks · Short answerHOTS / competency
A programmer places the whole body of main inside try { ... } catch (Exception e) { } with an empty catch block to "avoid crashes". Evaluate this practice with two disadvantages and a better approach. (3 marks)
Hint
Hidden errors.
Answer
Errors are hidden so the user and programmer do not know that something failed, and the program may continue with wrong data. A better approach is to catch specific exceptions, report or log a clear message and recover or stop safely.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.Always runs?Show answer
finally.
2.Division by zero exception?Show answer
ArithmeticException.
3.throws goes in?Show answer
The method header.
4.Invalid number string?Show answer
NumberFormatException.
5.Uncaught exception goes to?Show answer
The calling method.
Errors and Exceptions: try, catch, finally, throw, throws and Propagation: frequently asked questions
Can a try exist without catch?
Yes, with finally.
Can there be many catch blocks?
Yes.
Is IOException checked?
Yes, it must be handled or declared.
What does e.getMessage() give?
The exception's message.