OOPS LAB : EX NO 11: EXCEPTION HANDLING


                                            USER DEFINED EXCEPTIONS

Case Study 1: Online Banking System  (R NO :1 - 5)

Scenario:

A bank application must prevent invalid withdrawals.

Step-by-step Questions:

  1. Define a class InsufficientBalanceException derived from exception.
  2. Create a BankAccount class with balance as a private member.
  3. Write a method withdraw(amount) that:
    • Throws exception if amount > balance.
  4. Display a meaningful error message using what().
  5. Write a main() function to:
    • Accept withdrawal amount from user
    • Handle exception using try-catch
  6. Extend:
    • Add another exception for minimum balance violation.

Case Study 2: Student Result Processing System    (R NO :6 - 10)

Scenario:

Marks entered should be within valid range.

Step-by-step Questions:

  1. Create a user-defined exception class InvalidMarksException.
  2. Accept marks for 5 subjects.
  3. Throw exception if:
    • Marks < 0 or > 100
  4. Handle exception and prompt user to re-enter marks.
  5. Calculate average only if all inputs are valid.
  6. Extend:
    • Add exception for "Fail if any subject < 40".

Case Study 3: E-Commerce Order System   (R NO :11 - 15)

Scenario:

Order quantity must be valid.

Step-by-step Questions:

  1. Create exception class InvalidQuantityException.
  2. Create a class Product with:
    • product name, stock
  3. Write function placeOrder(quantity):
    • Throw exception if quantity <= 0 or exceeds stock
  4. Catch exception and display error.
  5. Update stock if order is successful.
  6. Extend:
    • Add exception for "OutOfStockException".

Case Study 4: Employee Salary System  (R NO :16 - 20)

Scenario:

Salary constraints must be validated.

Step-by-step Questions:

  1. Define InvalidSalaryException.
  2. Create Employee class with salary.
  3. Throw exception if:
    • Salary < minimum wage
  4. Handle exception in main().
  5. Display proper message.
  6. Extend:
    • Add exception for bonus exceeding salary.

Case Study 5: File Upload System   (R NO :21 - 25)

Scenario: 

File size limit must not exceed.

Step-by-step Questions:

  1. Create FileSizeTooLargeException.
  2. Accept file size and file type as input (in MB) (e.g., JPG, PNG, PDF)
  3. Throw exception if size > allowed limit (e.g., 10MB). and type not matched also 
  4. Handle exception  for file size extended and unsupported file type.and display message.

Case Study 6: ATM PIN Verification   (R NO :26 - 30)

Scenario:

User gets limited attempts.

Step-by-step Questions:

  1. Create InvalidPINException.
  2. Allow only 3 attempts to enter correct PIN.
  3. Throw exception after 3 failed attempts.
  4. Handle exception and block access.
  5. Extend:
    • Add exception for "CardBlockedException".

Case Study 7: Online Ticket Booking   (R NO :31 - 40)

Scenario:

Seats must be available.

Step-by-step Questions:

  1. Create SeatUnavailableException.
  2. Store total seats and booked seats.
  3. Throw exception if booking exceeds available seats.
  4. Handle exception gracefully.
  5. Extend:
    • Add waiting list logic.

Case Study 8: Library Management System   (R NO :41 - 50)

Scenario:

Book issuing rules.

Step-by-step Questions:

  1. Create BookNotAvailableException.
  2. Create class Library with book stock.
  3. Throw exception if book count = 0.
  4. Handle exception in main.
  5. Extend:
    • Add fine exception for late return.

Case Study 9: Login Authentication System   (R NO :51 - 55)

Scenario:

Invalid login attempts.

Step-by-step Questions:

  1. Create InvalidLoginException.
  2. Accept username and password.
  3. Throw exception if credentials mismatch.
  4. Handle exception and show error.
  5. Extend:
    • Lock account after multiple failures.

Case Study 10: Electricity Billing System   (R NO :56 - 61)

Scenario:

Invalid meter reading.

Step-by-step Questions:

  1. Create InvalidReadingException.
  2. Accept previous and current readings.
  3. Throw exception if:
    • Current < previous
  4. Handle exception and ask for re-entry.
  5. Extend:
    • Add exception for abnormal consumption.

 


Comments