Utkarsh Java Exp 3 (1)
Utkarsh Java Exp 3 (1)
Experiment - 3
Student Name : Utkarsh Sawarn UID: 22BCS16966
Branch: BE-CSE Section/Group: 630-B
Semester: 6th Date of Performance: 31/01/25
Subject Name: Java Subject Code: 22CSH-352
2. Implementation/Code:
a)
import java.util.Scanner;
try {
if (num < 0) {
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
} finally {
scanner.close();
}
b)
import java.util.Scanner;
try {
System.out.print("Enter PIN: ");
int enteredPin = scanner.nextInt();
if (enteredPin != PIN) {
throw new InvalidPinException("Error: Invalid PIN.");
}
balance -= withdrawAmount;
System.out.println("Withdrawal successful! Remaining Balance: " + balance);
try {
System.out.print("Enroll in Course: ");
String course = scanner.nextLine();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
System.out.print("Prerequisite: ");
String prerequisite = scanner.nextLine();
if (prerequisites.containsKey(course) &&
!prerequisites.get(course).equals(prerequisite)) {
throw new PrerequisiteNotMetException("Error: PrerequisiteNotMetException
- Complete " + prerequisites.get(course) + " before enrolling in " + course + ".");
}
3. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
(a)
(a)
(b) (c)
6. Learning Outcomes:
Exception Handling & Robust Code – Learn to use try-catch, throw, and
custom exceptions for handling errors like invalid input, insufficient balance,
and unmet prerequisites.
User Input & Decision Making – Gain experience in handling user inputs,
validating conditions (PIN check, balance check, prerequisites), and
controlling program flow.
OOP & Data Management – Understand object-oriented principles like
custom exception classes and use data structures (e.g., HashMap) for
managing enrollments dynamically.