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

    import java.util.Scanner;
    
    public class MyExceptionTest {
        
        public static void check(Square A) throws WrongException
        {
            if(A.getChang()<=0 && A.getKuan()<=0){
                throw new WrongException("长<=0,不合法, 宽<=0,也不合法");
            }
            if(A.getKuan()<=0){
                throw new WrongException("宽<=0,不合法");
            }
            if(A.getChang()<=0){
                throw new WrongException("长<=0,不合法");
            }
            A.CalcuArea();
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Square A=new Square();
            Scanner in =new Scanner(System.in);
            System.out.println("请输入矩形参数:
    "+"长: 宽:
    ");
            int Chang=in.nextInt();
            A.setChang(Chang);
            int kuan=in.nextInt();
            A.setKuan(kuan);
            
            try{        
                check(A);
                System.out.println(A.toString());
            }catch(WrongException e){
                System.out.println("报错啦
    "+e.getMessage());
                e.toString();
                e.printStackTrace();            
            }finally{
                System.out.println("谢谢使用,再见!");
            }
        }
    }
    
    class Square
    {    
        private int Chang,Kuan;
        private int Area;
    
        public int getChang() {
            return Chang;
        }
        public String toString() {
            return "矩形  [长=" + Chang + ", 宽=" + Kuan + ", 面积=" + Area + "]";
        }
    
        public void setChang(int chang) {
            Chang = chang;
        }
    
        public int getKuan() {
            return Kuan;
        }
    
        public void setKuan(int kuan) {
            Kuan = kuan;
        }
        public void CalcuArea()
        {
            Area=Chang*Kuan;
        }
    }
    
    class WrongException extends Exception {
        
        //定义一个Message
        String Message;
        
        //构造方法,设置Message
        public WrongException(String Message) {
            this.Message = Message;
        }
        
        //输出Message
        public String getMessage()
        {
            return Message;
        }
    }

    public class Test1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a = 100, b = 0, c;
            try{
                c = a / b;
                System.out.println("c=" + c);
            }catch(ArithmeticException e){
                System.out.println("catched!");
                System.out.println("catch ArithmeticException message:"+
                e.getMessage());
                System.out.println("catch ArithmeticException toSting():"
                        + e.toString());
                e.printStackTrace();//打印出现错误的地方
            }finally{
                System.out.println("finally!");
            }
        }
    
    }

    public class Test4 {
    
        public static void main(String[] args) {
            int a = 100, b = 2, c = 0;
            int[] x = { 10, 20, 30, 40, 50, 60, 70 };
            // 如果try写在for循环外面,异常后就跳出,将不再进入循环
            for (int i = 0; i <= 10; i++) {
                System.out.println("c=" + c);
                try {
                    c = a / b--;
                    System.out.println("x[" + i + "]=" + x[i]);
                } catch (ArithmeticException e) {
                    System.out.println("catched ArithmeticException message:" + e.getMessage());
                    System.out.println("catched ArithmeticException toSting():" + e.toString());
                    e.printStackTrace();
                } catch (ArrayIndexOutOfBoundsException e) {
                    System.out.println("catched ArrayIndexOutOfBoundsException!");
                } finally {
                    System.out.println("finally!");
                }
            }
        }
    
    }
    c=0
    x[0]=10
    finally!
    c=50
    x[1]=20
    finally!
    c=100
    catched ArithmeticException message:/ by zero
    catched ArithmeticException toSting():java.lang.ArithmeticException: / by zero
    java.lang.ArithmeticException: / by zero
        at Test4.main(Test4.java:11)
    finally!
    c=100
    x[3]=40
    finally!
    c=-100
    x[4]=50
    finally!
    c=-50
    x[5]=60
    finally!
    c=-33
    x[6]=70
    finally!
    c=-25
    catched ArrayIndexOutOfBoundsException!
    finally!
    c=-20
    catched ArrayIndexOutOfBoundsException!
    finally!
    c=-16
    catched ArrayIndexOutOfBoundsException!
    finally!
    c=-14
    catched ArrayIndexOutOfBoundsException!
    finally!
    //使用 throw 语句抛出异常、使用 throws 子句抛弃异常
    public class TestThrow1 {
        static void throwProcess(Object o) throws NullPointerException{// throws NullPointerException可不写
            if(o==null){
                throw new NullPointerException("空指针异常");//手动写抛出异常
            }
            //若抛出异常则不再执行函数下面的语句
            System.out.println(o+"哈哈呵呵");
        }
        public static void main(String[] args) {
            Object p=null;
            try{
                throwProcess(p);
            }catch(NullPointerException e){
                System.out.println("捕获:" + e);
            }
        }
    
    }

  • 相关阅读:
    单例模式中的懒汉式以及线程安全性问题
    单例模式中的饿汉模式
    自我管理的8个好习惯
    从java字节码角度看线程安全性问题
    工作上的建议
    从线程的优先级看饥饿问题
    多线程存在哪些风险
    DirectX SDK (June 2010)安装错误S1023,解决方法
    Microsoft DirectX SDK 2010 版本下载
    如果程序集是从 Web 上下载的,即使它存储于本地计算机,Windows 也会将其标记为 Web 文件,http://go.microsoft.com/fwlink/?LinkId=179545
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/10021896.html
Copyright © 2011-2022 走看看