Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Medium · Free

What is exception handling? Compare try/catch/finally across Java and Python.

FresherFundamentalsProgramming Fundamentals
Model answer

Exceptions: control flow for failures

An exception signals an error or unexpected state. try runs risky code; catch handles specific types; finally always runs for cleanup (close files). Uncaught exceptions unwind the call stack.

JavaPython
CheckedMust handle or declare (IOException)N/A—no checked exceptions
UncheckedRuntimeExceptionMost exceptions
Cleanuptry-with-resourcestry/finally or with
java
try (InputStream in = new FileInputStream(path)) {
  // read
} catch (IOException e) {
  throw new ServiceException("upload failed", e);
}
Prefer fail fast with context—wrap low-level errors with a message your API user understands.