Exception Handling in OOP Java Basics & Concepts
Introduction • Errors and exceptions are common during program execution. • Error → Irrecoverable (e.g., OutOfMemoryError). • Exception → Conditions that a program can handle (e.g., division by zero).
What is an Exception? • An exception is an event that disrupts the normal flow of a program. • Occurs during runtime. • Example: int a = 10 / 0; // ArithmeticException
Exception Handling • Mechanism to handle runtime errors. • Prevents abnormal program termination. • Provides meaningful error messages.
Keywords in Exception Handling • try → Defines block of code to be tested. • catch → Handles the exception. • finally → Executes code always (cleanup code). • throw → Used to throw an exception. • throws → Declares exceptions a method can throw.
Try-Catch Example • try { • int a = 10 / 0; • } catch (ArithmeticException e) { • System.out.println("Error: Division by zero!"); • }
Multiple Catch Blocks • try { • int arr[] = new int[5]; • arr[6] = 10; • } catch (ArithmeticException e) { • System.out.println("Math error"); • } catch (ArrayIndexOutOfBoundsException e) { • System.out.println("Array index error"); • }
Finally Block • try { • int a = 10 / 2; • } catch (Exception e) { • System.out.println("Error"); • } finally { • System.out.println("Always executed!"); • }
Throw and Throws • void checkAge(int age) throws ArithmeticException { • if(age < 18) { • throw new ArithmeticException("Not eligible to vote"); • } • }
Types of Exceptions • Checked Exceptions → Checked at compile time (e.g., IOException). • Unchecked Exceptions → Checked at runtime (e.g., NullPointerException). • Errors → Beyond programmer’s control (e.g., OutOfMemoryError).
Benefits of Exception Handling • Separates error-handling code from normal code. • Improves program reliability. • Provides meaningful messages to users.
Conclusion • Exception handling is essential in OOP Java. • Ensures smooth execution of programs. • Key tools: try, catch, finally, throw, throws.

Exception_Handling_in_Java object orineted programming.pptx

  • 1.
    Exception Handling inOOP Java Basics & Concepts
  • 2.
    Introduction • Errors andexceptions are common during program execution. • Error → Irrecoverable (e.g., OutOfMemoryError). • Exception → Conditions that a program can handle (e.g., division by zero).
  • 3.
    What is anException? • An exception is an event that disrupts the normal flow of a program. • Occurs during runtime. • Example: int a = 10 / 0; // ArithmeticException
  • 4.
    Exception Handling • Mechanismto handle runtime errors. • Prevents abnormal program termination. • Provides meaningful error messages.
  • 5.
    Keywords in ExceptionHandling • try → Defines block of code to be tested. • catch → Handles the exception. • finally → Executes code always (cleanup code). • throw → Used to throw an exception. • throws → Declares exceptions a method can throw.
  • 6.
    Try-Catch Example • try{ • int a = 10 / 0; • } catch (ArithmeticException e) { • System.out.println("Error: Division by zero!"); • }
  • 7.
    Multiple Catch Blocks •try { • int arr[] = new int[5]; • arr[6] = 10; • } catch (ArithmeticException e) { • System.out.println("Math error"); • } catch (ArrayIndexOutOfBoundsException e) { • System.out.println("Array index error"); • }
  • 8.
    Finally Block • try{ • int a = 10 / 2; • } catch (Exception e) { • System.out.println("Error"); • } finally { • System.out.println("Always executed!"); • }
  • 9.
    Throw and Throws •void checkAge(int age) throws ArithmeticException { • if(age < 18) { • throw new ArithmeticException("Not eligible to vote"); • } • }
  • 10.
    Types of Exceptions •Checked Exceptions → Checked at compile time (e.g., IOException). • Unchecked Exceptions → Checked at runtime (e.g., NullPointerException). • Errors → Beyond programmer’s control (e.g., OutOfMemoryError).
  • 11.
    Benefits of ExceptionHandling • Separates error-handling code from normal code. • Improves program reliability. • Provides meaningful messages to users.
  • 12.
    Conclusion • Exception handlingis essential in OOP Java. • Ensures smooth execution of programs. • Key tools: try, catch, finally, throw, throws.