• 异常处理(9.23)——————————课后练习


    package exception;
    /*
    建立exception包,编写TestException.java程序,主方法中有以下代码,
    确定其中可能出现的异常,进行捕获处理。
    */
    public class TestException {
    
        public static void main(String[] args) 
        {
            try 
            {
                for(int i=0;i<4;i++)
                {
                    int  k;
                    switch(i)
                    {
                    case 0:
                        int zero=0;
                        k=911/zero;
                        break;
                    case 1:
                        int  b[]=null;
                        k = b[0];
                        break;
                    case 2:
                        int c[]=new int[2];
                        k=c[9];
                        break;
                    case 3:
                        char  ch="abc".charAt(99);
                        break;
                    }
                }
            }
            catch(ArithmeticException e)
            {
                System.out.println(e.getMessage()+"	被除数不能为零");
            }
            catch(NullPointerException e)
            {
                System.out.println(e.getMessage()+"	空指针");
            }
            catch(ArrayIndexOutOfBoundsException e)
            {
                System.out.println(e.getMessage()+"	非法索引");
            }
            catch(StringIndexOutOfBoundsException e)
            {
                System.out.println(e.getMessage()+"	字符串索引越界");
            }
        }
    
    }

    package exception;
    
    import javax.naming.InsufficientResourcesException;
    
    
    public class CeshiBank 
    {
    
        public static void main(String[] args) throws InsufficientResourcesException 
        {
            Bank a = new Bank(100) ;
            a.withDrawal(150);
            a.withDrawal(-15);
        }
    
    }
    
    class Bank
    {
            //成员属性
            private double balance ;    
            
            //无参构造方法
            public Bank()
            {
                
            }
            //有参构造方法
            public Bank(double balance)
            {
                this.balance = balance ;
            }
            
            //判断然后抛出异常
            public void withDrawal(double dAmount) throws InsufficientResourcesException
            {
                if(dAmount < 0)
                {
                    throw new NagativeFundsException("取款数为负") ;
                }
                if(dAmount > balance)
                {
                    throw new InsufficientResourcesException("余额不足") ;
                }
                balance = balance - dAmount ;
            }    
    
    }
    
    //接收异常
    class InsufficientFundsException extends RuntimeException
    {
        public InsufficientFundsException(String msg)
        {
            super(msg) ;
        }
    }
    //接收异常
     class NagativeFundsException extends RuntimeException
     {
        public NagativeFundsException(String msg)
        {
            super(msg) ;
        }
     }

  • 相关阅读:
    November 13th 2016 Week 47th Sunday The 1st Day
    November 12th 2016 Week 46th Saturday
    November 11th 2016 Week 46th Friday
    November 10th 2016 Week 46th Thursday
    November 9th 2016 Week 46th Wednesday
    November 8th 2016 Week 46th Tuesday
    windows 7文件共享方法
    Win7无线网络共享设置方法
    常量指针和指针常量
    如何查找局域网的外网ip
  • 原文地址:https://www.cnblogs.com/20gg-com/p/5901432.html
Copyright © 2020-2023  润新知