您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
【第7章:异常的基本概念】_异常的其他概念
发布时间:2020-12-18 17:56:08编辑:雪饮阅读()
throws
throws关键字用来声明某个方法可能会有异常,即便没有异常,只要有这个关键字声明则调用者必须对这个方法进行异常处理。
throws声明某个方法的可能有异常时候必须指定一个异常类。
class Math{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}
public class Hello{
public static void main(String args[]){
new Math().div(2,1);
}
}
D:\>javac Hello.java
Hello.java:9: 未报告的异常 java.lang.Exception;必须对其进行捕捉或声明以便抛出
new Math().div(2,1);
^
1 错误
这里throws给Match类中的div方法声明了该方法可能会有Exception类的异常,而主方法中没有进行异常处理,所以就报错了。
所以只要进行异常捕获就好了
class Math{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}
public class Hello{
public static void main(String args[]){
try{
System.out.println("除法操作:"+ new Math().div(2,1));
}
catch(Exception e){
e.printStackTrace();
}
}
}
throws还有一个作用,就是用throws声明的方法中若有异常无需捕获会自动抛出到调用者让调用者处理,那么这个作业在主方法中依然有效,上面主方法必须对Math的div方法进行异常处理,这里如果主方法也被throws声明了,则主方法也无需处理了,那么就顺势交给了java的JVM虚拟机处理了。
class Math{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}
public class Hello{
public static void main(String args[]) throws Exception{
System.out.println("除法操作:"+ new Math().div(2,1));
}
}
D:\>javac Hello.java
D:\>java Hello
除法操作:2
真有异常的时候JVM会处理这个异常
class Math{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}
public class Hello{
public static void main(String args[]) throws Exception{
System.out.println("除法操作:"+ new Math().div(2,0));
}
}
D:\>javac Hello.java
D:\>java Hello
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Math.div(Hello.java:3)
at Hello.main(Hello.java:9)
throw
throw关键字用于抛出一个异常实例,异常实例一般构造时候传递一个异常信息(string)
public class Hello{
public static void main(String args[]){
throw new Exception("自己抛着玩");
}
}
D:\>javac Hello.java
Hello.java:3: 未报告的异常 java.lang.Exception;必须对其进行捕捉或声明以便抛出
throw new Exception("自己抛着玩");
^
1 错误
同样的,throw直接抛出则调用者必须捕获,这里在主方法中自己抛出而没有异常处理,所以最后就落到了java的JVM虚拟机来处理了。
所以主方法自己抛出的异常自己也要进行处理
public class Hello{
public static void main(String args[]){
try{
throw new Exception("自己抛着玩");
}
catch(Exception e){
System.out.println(e);
}
}
}
D:\>javac Hello.java
D:\>java Hello
java.lang.Exception: 自己抛着玩
throws、try、catch、throw、finally的综合应用
class Math{
public int div(int x,int y) throws Exception{
int tmp=0;
System.out.println("------计算开始-----");
try{
tmp=x/y;
}
catch(Exception e){
throw e;
}
finally{
System.out.println("------计算结束-----");
}
return tmp;
}
}
public class Hello{
public static void main(String args[]){
Math m=new Math();
try{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
System.out.println("除法操作:"+m.div(x,y));
}
catch(Exception e){
System.out.println("异常产生:"+e);
}
}
}
D:\>javac Hello.java
D:\>java Hello 2 0
------计算开始-----
------计算结束-----
异常产生:java.lang.ArithmeticException: / by zero
RuntimeException
上面看到有异常抛出就要在调用的地方进行异常处理,可是看看下面这个程序
public class Hello{
public static void main(String args[]){
int temp=Integer.parseInt(args[0]);
System.out.println(temp*temp);
}
}
D:\>javac Hello.java
D:\>java Hello
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Hello.main(Hello.java:3)
D:\>java Hello jk
Exception in thread "main" java.lang.NumberFormatException: For input string: "jk"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at Hello.main(Hello.java:3)
编译时候没有错误,但在运行时候就报错了,以NumberFormatException异常为例,因为NumberFormatException是RuntimeException的子类。RuntimeException异常以及抛出的其子类异常是不需要try/catch捕获的异常,不需要捕获,那么在编译期间也就不会报错了。者就是运行时异常。
自定义异常
自定义异常只需要继承Exception类型并在自己构造中将异常消息(String)通过super传递给Exception即可,catch时候也可以catch下Exception
class MyException extends Exception{
public MyException(String msg){
super(msg);
}
}
public class Hello{
public static void main(String args[]){
try{
throw new MyException("抛着玩玩");
}
catch(Exception e){
System.out.println("出现异常了:"+e);
}
}
}
断言
断言是指我确定某个数据或者某个结果
断言是jdk1.4之后才有的
我个人认为比如说对接某个第三方接口时候某个字段的值或者该接口的数据结构只有文档并且该文档可靠性还不能保证之下,或者这个文档的意思说的不太到位,但我根据我自己的意思理解出了一个结构,假定(断言)它文档就是指的这个意思,最后若在此结构上出现了问题那么根据责任链就能很快定位到这里来。
public class Hello{
public static void main(String args[]){
int x[]={1,2,3};
//断言这里这个数组长度为0
assert x.length==0;
}
}
D:\>javac Hello.java
D:\>java Hello
这里断言了目标数组长度为0,实际不为0,但也不会报错。
这是因为断言不是这样用的而是和验证断言配合用的
验证断言
用”java -ea 类名”语法格式就是对指定类中main方法中的断言进行验证,至少这里是main方法。
D:\>java -ea Hello
Exception in thread "main" java.lang.AssertionError
at Hello.main(Hello.java:5)
所以这里把断言修正下
public class Hello{
public static void main(String args[]){
int x[]={1,2,3};
//断言这里这个数组长度为0
assert x.length==3;
}
}
D:\>javac Hello.java
D:\>java -ea Hello
然后就会发现断言就没有错误报出了。
自定义断言错误后的错误提示
public class Hello{
public static void main(String args[]){
int x[]={1,2,3};
//断言这里这个数组长度为0
assert x.length==0 : "你的断言错误,数组长度实际不为0";
}
}
关键字词:java,异常,断言