Java异常 Exception类及其子类
2024-09-01 16:39:22
C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象。
Java异常处理通过5个关键字try、catch、finally、throw、throws进行管理。基本过程是用try包住要监视的语句,如果在try内出现异常,则异常会被抛出,catch中捕获抛出的异常并做处理,finally一定会完成未尽事宜。
练习:
package com.swift; public class Exception1
{
public static void main(String args[]){
System.out.println("=========计算开始=========");
try{
int i=Integer.parseInt(args[0]);
int j=Integer.parseInt(args[1]);
int temp=i/j;
System.out.println("计算结果:"+ temp);
}catch(ArithmeticException e){
System.out.println("出现了数学异常 "+ e);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("出现了数组异常 "+ e);
}catch(NumberFormatException e){
System.out.println("出现了格式异常 "+ e);
}catch(Exception e){
System.out.println("其他异常 "+e);
}finally{
System.out.println("不管是否有异常,我都执行。");
}
System.out.println("=========计算结束=========");
}
}
throws标识此方法出异常但不处理,谁调谁处理。
package com.swift; public class Exception3
{
public static void main(String args[]) throws Exception{
Operate o=new Operate();
System.out.println("=============计算之前=============="); int temp=o.div(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
System.out.println("计算结果"+temp);
System.out.println("=============计算之后==========");
}
}
class Operate
{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}
如果main也throws了,异常抛给了JVM,其实你一个throws不写,JVM依然会默默地处理着异常
注意:一旦方法throws,main必须有回应,否则编译不过。main继续抛相当于出异常之后的程序戛然而止(没有"=============计算之后=========="),这时可用catch捕获抛来的异常进行处理。
package com.swift; public class Exception3
{
public static void main(String args[]) throws Exception{
Operate o=new Operate();
System.out.println("=============计算之前==============");
try {
int temp=o.div(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
System.out.println("计算结果"+temp);
}catch(Exception e){
System.out.println("出现了什么异常情况 "+e);
}
System.out.println("=============计算之后==========");
}
}
class Operate
{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}
异常被处理,计算前后正常输出。
throw与throws不同,throw抛出的不是异常类,而是对象,可以人造的对象。
package com.swift; public class Exception4 {
public static void main(String args[]) {
try {
throw new Exception("抛着玩呢 "); // 抛出人造异常对象
} catch (Exception e) { // 再捕捉这个对象
System.out.println("你抛的是什么呢?"+e);
}
} }
如果不想抛Exception类的对像,要抛自己的
package com.swift; public class Exception5 {
public static void main(String args[]) {
try {
throw new MyException("自定义异常。");
} catch (Exception e) {
System.out.println("这个异常是 " + e); // e.printStackTrace();
}
}
}
//通过继承实现自己的异常类
class MyException extends Exception {
public MyException(String msg) {
//把参数传给父类
super(msg);
}
}
最新文章
- 关于rails里集成测试assert_template的写法
- [Cocos2d-x For WP8]ActionManager动作管理
- quartz 实例记录
- android 应用架构随笔一(架构搭建)
- 简化对象extend拓展
- [转]PHP5.5安装PHPRedis扩展
- Nginx的介绍和使用
- Spring面试题汇总
- request和response的中文乱码问题
- Matlab入门学习(文件读写)
- win10 uwp 列表模板选择器
- 设置某个类使用或者禁用ARC
- Golang 交叉编译 window/linux 文件
- sklearn_随机森林random forest原理_乳腺癌分类器建模(推荐AAA)
- Maven不能下载SNAPSHOT包但是能下载RELEASE包的解决办法
- centos7换源
- 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)
- 20155334 《网络攻防》Exp5 MSF基础应用
- Nginx+Tomcat+Memcache实现负载均衡及Session共享
- 对IT行业的看法和对软件工程的理解
热门文章
- P5175 数列(矩阵加速)
- PV(Pageviews)、访问(Visits)和访问者(Visitors)的区别
- [Xcode 实际操作]七、文件与数据-(21)ARKit增强现实框架的使用
- 即时通讯新手入门:一文读懂什么是Nginx?它能否实现IM的负载均衡?
- C#主从表查询
- c++中初始化列表的初始化变量顺序问题
- Codeforces 1138B(列方程枚举)
- Xenu使用随记
- 108 Convert Sorted Array to Binary Search Tree 将有序数组转换为二叉搜索树
- 使用express+mongoDB搭建多人博客 学习(2)路由与模板