java 异常练习

Posted 厂公

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 异常练习相关的知识,希望对你有一定的参考价值。

建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。

package com.yichang;

public class TestException {
    public static void main(String[] args) {
        for (int i = 0; i < 4; i++) {
            int k;
            switch (i) {
            case 0:
                try{
                int zero = 0;
                k = 911 / zero;
                break;}
                catch(Exception ex)
                {
                    ex.printStackTrace();
                    System.out.println("运行出现算数异常"+ex.getMessage());
                }
            case 1:
                try{
                int b[] = null;
                k = b[0];}
                catch(Exception ex)
                {
                    ex.printStackTrace();
                    System.out.println("运行出现空指针异常"+ex.getMessage());
                }
                break;
            case 2:
                try{
                int c[] = new int[2];

                k = c[9];}
                catch(Exception ex)
                {
                    ex.printStackTrace();
                    System.out.println("运行出现异常"+ex.getMessage());
                }
                break;
            case 3:
                try{
                char ch = "abc".charAt(99);}
                catch(Exception ex)
                {
                    ex.printStackTrace();
                    System.out.println("运行出现异常"+ex.getMessage());
                }

                break;
            }
        }

    }

}

建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。

package exception;

public class InsufficientFundsException extends Exception {

    public InsufficientFundsException(String string) {
        
    }

    public InsufficientFundsException() {
        System.out.println("余额不足");
    }

}
package exception;

public class NagativeFundsException extends Exception {

    public NagativeFundsException(String string) {
        
    }

    public NagativeFundsException() {
        System.out.println("取款不能为负数");
    }

}
package exception;

public class Bank {
    private double balance;

    public Bank(double balance) {
        super();
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    double withDrawal(double dAmount) throws Exception
    {
        if(dAmount>balance)
        {
            throw new InsufficientFundsException();
        }
        if(dAmount<0)
        {
            throw new NagativeFundsException();
        }
        return this.balance;
    }
    public static void main(String[] args) {
        Bank b=new Bank(100);
        try {
            b.withDrawal(150);
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        try {
            b.withDrawal(-15);
        } catch (Exception e) {
            
            e.printStackTrace();
        }
    }

}

 

以上是关于java 异常练习的主要内容,如果未能解决你的问题,请参考以下文章

java 异常练习题2

Java-异常处理练习

Java 异常练习

java课堂练习——异常

Java异常处理练习题

java 反射代码片段