Fundamentals · Medium · Free
What is exception handling? Compare try/catch/finally across Java and Python.
FresherFundamentalsProgramming Fundamentals
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.
| Java | Python | |
|---|---|---|
| Checked | Must handle or declare (IOException) | N/A—no checked exceptions |
| Unchecked | RuntimeException | Most exceptions |
| Cleanup | try-with-resources | try/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.