Java学习总结:19

throws关键字

throws关键字主要在方法定义上使用,表示此方法中不进行异常的处理,而是交给被调用处处理。

例:使用throws

package com.study.Demo;

class MyMath{
    public static int div(int x,int y)throws Exception{	//此方法不处理异常
        return x/y;
    }
}
public class Test4 {
    public static void main(String args[]){
        try{	//div()方法抛出异常,必须明确进行异常处理
            System.out.println(MyMath.div(10,5));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//结果
//2

提示:主方法上也可以使用throws抛出。

例:在主方法上使用throws

package com.study.Demo;

class MyMath{
    public static int div(int x,int y)throws Exception{	//此方法不处理异常
        return x/y;
    }
}
public class Test4 {
    public static void main(String args[]) throws Exception{
    //表示此异常产生后会直接通过主方法抛出,代码中可以不强制使用异常处理
        System.out.println(MyMath.div(10,0));
    }
}
//结果
//Exception in thread "main" java.lang.ArithmeticException: / by zero
//	at com.study.Demo.MyMath.div(Test4.java:5)
//	at com.study.Demo.Test4.main(Test4.java:10)

throw关键字

throw关键字可以由用户手工抛出一个实例化对象。

例:手工抛出异常

package com.study.Demo;

public class Test4 {
    public static void main(String args[]){
        try{
            throw new Exception("自己定义的异常");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//结果
//java.lang.Exception: 自己定义的异常
//	at com.study.Demo.Test4.main(Test4.java:11)

异常处理的标准格式

例:

package com.study.Demo;

class MyMath{
    public static int div(int x,int y)throws Exception{	//出现异常要交给被调用处输出
        System.out.println("计算开始");
        int result=0;
        try{
            result=x/y;
        }catch (Exception e){
            throw e;	//向上抛
        }finally {
            System.out.println("计算结束");
        }
        return result;
    }
}
public class Test3 {
    public static void main(String args[]){
        try {
            System.out.println(MyMath.div(10,0));	//被调用处处理异常
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

RuntimeException类

RuntimeException类的特征:
程序在编译时不会强制性地要求用户处理异常,用户可以根据自己的需要有选择地进行处理,但是如果没有处理又发生了异常,将交由JVM默认处理。

例:

package com.study.Demo;

public class Test4 {
    public static void main(String args[]){
        int temp=Integer.parseInt("100");
        System.out.println(temp);
    }
}
//结果
//100

此代码正常运行,但如出现异常,就交由JVM进行默认处理。

assert关键字

assert关键字的主要功能是进行断言。断言指的是程序执行到某行之后,其结果一定是预期的结果。
例:观察断言的使用

package com.study.Demo;

public class Test5 {
    public static void main(String args[]){
        int num=10;
        //假设中间可能经过了20行代码来操作num的内容,期望的内容应该是20
        assert num==20:"num的内容不是20";	//进行断言操作
        System.out.println("num="+num);
    }
}
//结果
//num=10

结果没有出现异常,这是因为Java默认情况下是不开启断言的。如果想要启用断言,需要以下这样做:

1.打开相应的控制面板
在这里插入图片描述
2.输入-ea
在这里插入图片描述

自定义异常

用户自己开发的异常类。
如果要想实现自定义异常类,只需要继承Exception或RuntimeException父类即可。
例:定义AddException

package com.study.Demo;

class AddException extends Exception{	//此异常类要强制处理
    public AddException(String msg){
        super(msg);	//调用父类构造
    }
}
public class Test5 {
    public static void main(String args[]){
        int num=20;
        try{
            if(num>10){	//出现了错误,应该产生异常
                throw new AddException("数值传递的过大");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//结果
//com.study.Demo.AddException: 数值传递的过大
//	at com.study.Demo.Test5.main(Test5.java:13)
发布了49 篇原创文章 · 获赞 25 · 访问量 1528

猜你喜欢

转载自blog.csdn.net/weixin_45784666/article/details/104414321