问问图中紫色标注部分要怎么实现 图书管理系统中的一个问题 在第一部分代码case2 的case2里 就是说现在要怎么解决那个异常

img

import java.util.Scanner;

public class CLI {
public static void main(String[] args) {
Library l = new Library("UIC Library");
int i = 0;
while (i != 6) {
i = readPosInt("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");

        switch (i) {
        case 1:
            System.out.println("Total number of borrowed books:" +l.totalBorrowedBooks());
            break;
        case 2:
            int j = readPosInt("Type the user role (lender:1 borrower:2):");
            switch (j) {
            case 1:
                String str1 = readLine("Enter the name of the user:");
                int k = readPosInt("Enter the initial number of borrowed books:");
                System.out.println("Lender " + str1 +" lending "+ k + " book(s) has been added.");
                Lender ll=new Lender(str1,k);
                l.addUser(ll);
                break;
            case 2:
                String str2 = readLine("Enter the name of the user:");
                int p = readPosInt("Enter the initial number of borrowed books:");
                System.out.println("Borrower " + str2 +" borrowing "+ p + " book(s) has been added.");
                
                /*这里要怎么实现????
                Borrower bb = new Borrower(str2,p);
                l.addUser(bb);
                try {
                    Borrower b=new Borrower(str2,-1);
                } catch (NotALenderException e) {
                    System.out.println("BUG!This must never happen!");
                    System.exit(1);
                }
                l.addUser(bb);*/
                break;
            default:
                System.out.println("Unknown action!");
                break;
            }
            break;
        case 3:
            System.out.println(3);
            break;
        case 4:
            System.out.println(4);
            break;
        case 5:
            System.out.println(5);
            break;
        case 6:
            System.out.println("Goodbye!");
            System.exit(0);
            break;
        default:
            System.out.println("Unknown action!");
            break;
        }
    }
    
}

public static String readLine(String str) {
    System.out.print(str);
    Scanner scanner = new Scanner(System.in);
    return scanner.nextLine();
}

public static int readPosInt(String str) {
    while (true) {
        try {
            System.out.print(str);
            Scanner scanner = new Scanner(System.in);
            int i = scanner.nextInt();
            if (i < 0) {
                System.out.println("Positive integers only!");
                continue;
            }
            return i;
        } catch (Exception e) {
            System.out.println("You must type an integer!");
        }

    }
}

}

public class Borrower extends User {
public Borrower(String name, int book) throws NotALenderException {
super(name, book);
if (this.getBook() <= 0) {
throw new NotALenderException("A new borrower cannot lend books.");
} else {
super.setBook(super.getBook());
}
}

@Override
public void moreBook(int number) throws NotALenderException {
    if (getBook() >= 0) {
        super.setBook(super.getBook() + number);
    } else {
        throw new NotALenderException("A new borrower cannot lend" + (-(getBook() + number)) + "book(s).");
    }
}

}

import java.util.Scanner;

public class CLI {
public static void main(String[] args) {
Library l = new Library("UIC Library");
int i = 0;
while (i != 6) {
i = readPosInt("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");

        switch (i) {
        case 1:
            System.out.println("Total number of borrowed books:" +l.totalBorrowedBooks());
            break;
        case 2:
            int j = readPosInt("Type the user role (lender:1 borrower:2):");
            switch (j) {
            case 1:
                String str1 = readLine("Enter the name of the user:");
                int k = readPosInt("Enter the initial number of borrowed books:");
                System.out.println("Lender " + str1 +" lending "+ k + " book(s) has been added.");
                Lender ll=new Lender(str1,k);
                l.addUser(ll);
                break;
            case 2:
                String str2 = readLine("Enter the name of the user:");
                int p = readPosInt("Enter the initial number of borrowed books:");
                System.out.println("Borrower " + str2 +" borrowing "+ p + " book(s) has been added.");

                try {
                    Borrower bb = new Borrower(str2,p);
                    l.addUser(bb);
                } catch (NotALenderException e) {
                    System.out.println("BUG!This must never happen!");
                    System.exit(1);
                }

                break;
            default:
                System.out.println("Unknown action!");
                break;
            }
            break;
        case 3:
            System.out.println(3);
            break;
        case 4:
            System.out.println(4);
            break;
        case 5:
            System.out.println(5);
            break;
        case 6:
            System.out.println("Goodbye!");
            System.exit(0);
            break;
        default:
            System.out.println("Unknown action!");
            break;
        }
    }
    
}
 
public static String readLine(String str) {
    System.out.print(str);
    Scanner scanner = new Scanner(System.in);
    return scanner.nextLine();
}
 
public static int readPosInt(String str) {
    while (true) {
        try {
            System.out.print(str);
            Scanner scanner = new Scanner(System.in);
            int i = scanner.nextInt();
            if (i < 0) {
                System.out.println("Positive integers only!");
                continue;
            }
            return i;
        } catch (Exception e) {
            System.out.println("You must type an integer!");
        }
 
    }
}
}

public class Borrower extends User {
public Borrower(String name, int book) throws NotALenderException {
super(name, book);
if (this.getBook() <= 0) {
throw new NotALenderException("A new borrower cannot lend books.");
} else {
super.setBook(super.getBook());
}
}

@Override
public void moreBook(int number) throws NotALenderException {
    if (getBook() >= 0) {
        super.setBook(super.getBook() + number);
    } else {
        throw new NotALenderException("A new borrower cannot lend" + (-(getBook() + number)) + "book(s).");
    }
}
}

如有帮助,请采纳,十分感谢!

看紫色部分的内容意思是要求main函数处理这个异常,那么将main里的内容放入try,catch中

public static void main(String[] args) {
try{
  原先main中的代码
}catch(NotALenderException){
  System.out.println("BUG!This must never happen!");
  System.exit(1);
}
}