Python 捕获线程中的异常

import sys
from threading import Thread


def install_thread_excepthook():

    init_original = Thread.__init__

    def init(self, *args, **kwargs):
        init_original(self, *args, **kwargs)
        run_original = self.run

        def run_with_except_hook(*a, **kw):
            try:
                run_original(*a, **kw)
            except (KeyboardInterrupt, SystemExit):
                raise
            except:
                sys.excepthook(*sys.exc_info())

        self.run = run_with_except_hook

    Thread.__init__ = init
发布了23 篇原创文章 · 获赞 84 · 访问量 2万+
展开阅读全文

新手在线等~Exception in thread ~

10-08

package banking; public class Account { protected double balance; public Account(double balance) { super(); this.balance = balance; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public boolean deposit(double amt) { balance+=amt; return true; } public boolean withdraw(double amt) { if(amt<=balance) { balance -= amt; }else { return false; } return true; } } package banking; public class SavingAccount extends Account { private double interestRate; public SavingAccount(double balance, double interestRate) { super(balance); this.interestRate = interestRate; } public double getInterestRate() { return interestRate; } } package banking; public class CheckingAccount extends Account { private double overdraftProtection; public CheckingAccount(double balance) { super(balance); } public CheckingAccount(double balance, double overdraftProtection) { super(balance); this.overdraftProtection = overdraftProtection; } public boolean withdraw(double amt) { if(amt<=balance) { balance -= amt; return true; }else { if(overdraftProtection>= amt - balance) { overdraftProtection -= amt - balance; balance = 0; }else { return false; } } return true; } } package banking; public class Customer { private String lastName; public String getLastName() { return lastName; } public String getFirstName() { return firstName; } private String firstName; Account[] accounts; int numberOfAccounts; public Customer(String L,String F) { lastName = L; firstName =F; } public Customer() { accounts = new Account[2]; } public void addAccount(Account account) { accounts[numberOfAccounts++]=account; } public Account getAccounts(int index) { return accounts [index]; } public int getNumberOfAccounts() { return numberOfAccounts; } } package banking; public class Bank { private Customer[] customers; private int numberOfCustomers; public Bank() { customers = new Customer[5]; } public Customer getCustomers(int index) { return customers[index]; } public int getNumberOfCustomers() { return numberOfCustomers; } public void addCustomer(String L,String F) { Customer customer = new Customer(L,F); customers[numberOfCustomers]=customer; numberOfCustomers++; } } package banking; public class TestBanking { public static void main(String[] args) { Bank bank = new Bank(); Customer customer; bank.addCustomer("Jane", "Smith"); customer = bank.getCustomers(0); customer.addAccount(new SavingAccount(500,0.05)); customer.addAccount(new CheckingAccount(200,400)); bank.addCustomer("oven", "Bryant"); customer = bank.getCustomers(1); customer.addAccount(new CheckingAccount(200)); bank.addCustomer("Tim", "Soley"); customer=bank.getCustomers(2); customer.addAccount(new SavingAccount(1500,0.05)); customer.addAccount(new CheckingAccount(200)); bank.addCustomer("Maria", "Soley"); customer = bank.getCustomers(3); customer.addAccount(bank.getCustomers(2).getAccounts(1)); customer.addAccount(new SavingAccount(150,0.05)); for(int i = 0;i<bank.getNumberOfCustomers();i++) { customer = bank.getCustomers(i); System.out.println(); System.out.println("Customer "+customer.getLastName()+customer.getFirstName()); } } } 错误是Exception in thread "main" java.lang.NullPointerException at banking.Customer.addAccount(Customer.java:28) at banking.TestBanking.main(TestBanking.java:13) 不明白哪里错了,刚刚自学JAVA的小萌新,希望能够详细解答,并给出修改方案~ 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览