Java Exception Notes

- 1 min

Exception Hierarchy

Throwable

Example of try-with-resources

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadData {
    public static void main(String[] args) {
        try (FileReader fr = new FileReader("file path")) {
            System.out.println(fr.nextLine());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Example

import java.io.*;

public class InsufficientFundsException extends Exception {
    private double amount;
    
    public InsufficientFundsException(double amount) {
        this.amount = amount;
    }
    
    public double getAmount() {
        return this.amount;
    }
}

public class CheckingAccount {
    private double balance;
    private int id;

    public CheckingAccount(int x) {
        this.id = x;
    }

    public void deposit (double amount) {
        this.balance += amount;
    }

    public void withdraw(double amount) throws InsufficientFundsException {
        if (this.balance >= amount) {
            balance -= amount;
        } else {
            double need = amount - balance;
            throw new InsufficientFundsException(need);
        }
    }

    public double getBalance() {
        return this.balance;
    }

    public int getId() {
        return this.id;
    }
}

public class BankDemo {
    public static void main(String[] args) {
        CheckingAccount myAccount = new CheckingAccount(1);
        myAccount.deposit(200.0);

        try {
            myAccount.withdraw(100.0);
            myAccount.withdraw(300.0);
        } catch (InsufficientFundsException e) {
            e.printStackTrace();
        }
    }
}

rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora