0% found this document useful (0 votes)
17 views3 pages

Week 4 KCPD

The document contains 3 Java code examples that demonstrate different exception handling concepts: 1. The first example shows how to define and throw a custom exception called MyException when a negative number is entered. 2. The second example defines a BankAccount class that throws an InsufficientFundsException if a withdrawal exceeds the available balance. 3. The third example demonstrates try-with-resources, multi-catch exceptions, and exception propagation by defining methods that throw different exceptions and calling them within try-catch blocks.

Uploaded by

vnraids
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Week 4 KCPD

The document contains 3 Java code examples that demonstrate different exception handling concepts: 1. The first example shows how to define and throw a custom exception called MyException when a negative number is entered. 2. The second example defines a BankAccount class that throws an InsufficientFundsException if a withdrawal exceeds the available balance. 3. The third example demonstrates try-with-resources, multi-catch exceptions, and exception propagation by defining methods that throw different exceptions and calling them within try-catch blocks.

Uploaded by

vnraids
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

a. Write a Java program to implement user defined exception handling.

class MyException extends Exception {


public MyException(String message) {
super(message);
}
}
public class UserDefinedException {
public static void main(String[] args) {
int number = 0;
try {
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter a positive number: ");
number = scanner.nextInt();
scanner.close();
if (number < 0) {
throw new MyException("Negative number entered");
}
System.out.println("The number is " + number);
}
catch (MyException e) {
System.out.println(e.getMessage());
}
}
}

b.Write a Java program to throw an exception “Insufficient Funds” while withdrawing


the amount in the user account.
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
class BankAccount {
private String owner;
private double balance;
private double minimumBalance;
public BankAccount(String owner, double balance, double minimumBalance) {
this.owner = owner;
this.balance = balance;
this.minimumBalance = minimumBalance;
}

public String getOwner() {


return owner;
}

public double getBalance() {


return balance;
}
public double getMinimumBalance() {
return minimumBalance;
}
public void withdraw(double amount) throws InsufficientFundsException {
if (amount <= 0) {
throw new IllegalArgumentException("Invalid withdrawal amount");
}
if (amount > balance - minimumBalance) {
throw new InsufficientFundsException("Insufficient funds");
}
balance -= amount;
}
}
class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount("Alice", 10000, 1000);
try {
account.withdraw(5000);
System.out.println("Withdrawal successful");
System.out.println("New balance: " + account.getBalance());
} catch (InsufficientFundsException e) {
System.out.println("Withdrawal failed");
System.out.println(e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println("Withdrawal failed");
System.out.println(e.getMessage());
}
}
}

c. Write a Java program to implement Try-with Resources, Multi-catch Exceptions,


and
Exception Propagation Concepts?

class Demo {
public static void method1() throws IOException {
throw new IOException("IO exception occurred");
}
public static void method2() throws ArithmeticException {
throw new ArithmeticException("Arithmetic exception occurred");
}
public static void method3() throws IOException, ArithmeticException {
method1();
method2();
}
public static void method4() throws IOException {
try (FileWriter fw = new FileWriter("output.txt")) {
fw.write("Hello world");
}
}
public static void main(String[] args) {
try {
method4();
System.out.println("File written successfully");
} catch (IOException e) {
System.out.println("IO exception: " + e.getMessage());
}
try {
method3();
} catch (IOException | ArithmeticException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}

You might also like