zoukankan      html  css  js  c++  java
  • Java-异常处理练习

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

    package Yichang;
    
    public class Text {
    
        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;
            
            }
            catch(ArithmeticException e){
                System.out.println("输入有误");
            }
            break;
            case 1:
            try{
            int  b[]=null;
            k = b[0];
            
            }catch(NullPointerException e)
            {
                System.out.println("空指针异常");
            }
            break;
                    case 2:
            try{
            int c[]=new int[2];
    
            k=c[9];
            
            }catch(ArrayIndexOutOfBoundsException e)
            {
                System.out.println("索引超出异常");
            }
            break;
                    case 3:
            try{
            char ch="abc".charAt(99);
            
            
            }catch(Exception e)
            {
                e.printStackTrace();
                System.out.println("收取字符超出");
            }
            break;
                }
            }
    
    
        }
    
    }

    结果:


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

    package Yichang;
    
    public class InsufficientFundsException extends Exception {
        public String getMessage()
        {
            return "余额不足";
        }
    }
    package Yichang;
    
    public class NagativeFundsException extends Exception {
     public String getMessage()
     {
         return "取款不能为负数";
     }
    }
    package Yichang;
    
    public class Test01 {
        private double balance;
    
        public Test01(double balance) {
            super();
            this.balance = balance;
        }
        public void withDrawal(double dAmount)throws InsufficientFundsException,NagativeFundsException
        {
            if(dAmount>balance)
            {
                throw new InsufficientFundsException();
            }
            if(dAmount<0)
            {
                throw new NagativeFundsException(); 
            }
        }
        
        public static void main(String[] args){
            Test01 t=new Test01(100);
            try{
            t.withDrawal(150);
            }catch(Exception e){
                e.printStackTrace();
            }
            try{
                t.withDrawal(-10);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    结果:

  • 相关阅读:
    Linux 多进程锁的几种实现方案
    Linux man手册没有pthread_mutex_init的解决办法
    IP地址结构信息与字符串相互转化:inet_pton和inet_ntop, etc.
    Linux 将计算md5值功能做成md5命令
    Unix/Linux inet守护进程
    Unix/Linux syslogd守护进程 & 日志记录syslog
    UNP 学习笔记 #11 名字与地址转换
    git 使用总结
    AUPE 输出致标准错误的出错函数分析与实现 err_sys, err_quit, err_doit etc.
    Linux C常见数I/O函数比较: printf, sprintf, fprintf, write...
  • 原文地址:https://www.cnblogs.com/tfl-511/p/5902696.html
Copyright © 2011-2022 走看看