Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Exception Handling

Lesson 29 of 39 in the free Java notes on Siksha Sarovar, written by Rohit Jangra.

Exception Definition

An unexpected problem/event that occurs during the execution of a program (Runtime) which stops the normal flow. • Root Class: java.lang.Throwable. • Object Class: Parent of Throwable.

Hierarchy

Throwable

  1. Error (Hardware/System level, e.g., StackOverflowError).
  2. Exception:
  3. Checked (Compile-time): Compiler forces handling (e.g., FileNotFoundException, IOException). • Unchecked (Runtime): Compiler ignores (e.g., ArithmeticException, NullPointerException). • Note: RuntimeException and its children are Unchecked. All others are Checked.

Exception Handling Keywords

  1. try: Block consisting of code that might raise an exception.
  2. catch: Block used to handle the exception thrown by try.
  3. • Can have multiple catch blocks (Child first, Parent last).

  4. finally: Block that always executes (even if exception occurs or handled).
  5. • Used for resource cleanup (Closing DB, Streams).

  6. throw: Used to explicitly throw an exception object. throw new ArithmeticException("Msg");
  7. throws: Used in method signature to declare/propagate checked exceptions. void m() throws IOException

Exception Propagation

Movement of exception object from called method to calling method. Unchecked exceptions propagate automatically. Checked exceptions need throws.

Custom Exception

User defined exceptions.

  1. Checked: Extend Exception.
  2. Unchecked: Extend RuntimeException.