首页 技术 正文
技术 2022年11月14日
0 收藏 936 点赞 3,599 浏览 1920 个字

抛出处理

定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理。

功能内部不想处理,或者处理不了。就抛出使用throw new Exception(“除数不能为0”); 进行抛出。抛出后需要在函数上进行声明,告知调用函数者,我有异常,你需要处理如果函数上不进行throws 声明,编译会报错。例如:未报告的异常 java.lang.Exception;必须对其进行捕捉或声明以便抛出throw new Exception("除数不能为0");

public static void div(int x, int y) throws Exception { // 声明异常,通知方法调用者。        if (y == 0) {
throw new Exception("除数为0"); // throw关键字后面接受的是具体的异常的对象
}
System.out.println(x / y);
System.out.println("除法运算");
}

5:main方法中调用除法功能

调用到了一个可能会出现异常的函数,需要进行处理。

1:如果调用者没有处理会编译失败。

如何处理声明了异常的函数。

1:try{}catch(){}

public static void main(String[] args) {    try {
div(2, 0);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("over");}
    public static void div(int x, int y) throws Exception { // 声明异常,通知方法调用者。        if (y == 0) {
throw new Exception("除数为0"); // throw关键字后面接受的是具体的异常的对象
}
System.out.println(x / y);
System.out.println("除法运算");
}
}

2:继续抛出throws

class Demo {    public static void main(String[] args) throws Exception {
div(2, 0);
System.out.println("over");
} public static void div(int x, int y) throws Exception { // 声明异常,通知方法调用者。
if (y == 0) {
throw new Exception("除数为0"); // throw关键字后面接受的是具体的异常的对象
} System.out.println(x / y);
System.out.println("除法运算");
}
}

throw和throws的区别

1.相同:都是用于做异常的抛出处理的。

2.不同点:

1.使用的位置: throws 使用在函数上,throw使用在函数内

2.后面接受的内容的个数不同:

1.throws 后跟的是异常类,可以跟多个,用逗号隔开。

2.throw 后跟异常对象。

//throws 处理
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
obj.wait(); }public static void main(String[] args) { //try catch 处理
Object obj = new Object();
try {
obj.wait();
} catch (InterruptedException e) { e.printStackTrace();
} }

总结

1.try语句不能单独存在,可以和catch、finally组成 try…catch…finally、try…catch、try…finally三种结构。

2.catch语句可以有一个或多个,finally语句最多一个,try、catch、finally这三个关键字均不能单独使用。

3.try、catch、finally三个代码块中变量的作用域分别独立而不能相互访问。如果要在三个块中都可以访问,则需要将变量定义到这些块的外面。

4.多个catch块时候,Java虚拟机会匹配其中一个异常类或其子类,就执行这个catch块,而不会再执行别的catch块。(子类在上,父类在下)。

5.throw语句后不允许有紧跟其他语句,因为这些没有机会执行。

6.如果一个方法调用了另外一个声明抛出异常的方法,那么这个方法要么处理异常,要么声明抛出。


【正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个“顶”字,你就顺手把它点了吧(要先登录CSDN账号哦 )】


—–乐于分享,共同进步!

—–更多文章请看:http://blog.csdn.net/duruiqi_fx


相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905