当前位置:网站首页>异常的概念与处理
异常的概念与处理
2022-07-26 10:31:00 【一尾流鸢cd】
1、概念
异常是导致程序中断的的一种指令流。
Thowable类是Java语言中所有错误或异常的超类。
2、Throwable的分类
Error:
错误,致命,不能恢复的错误,程序员很难处理
Exception:
异常,可以处理,采用异常处理机制进行处理
处理的两种方式:显示处理(try{}catch(Exception){})
不显示处理(throw,throws)
3、异常处理机制
try{}catch(){}finally{}
注意:
- 异常的处理只能从上到下由细到粗,不能由粗到细
- try catch finally三者不能分别存在,但三者可以并存(try与catch,try与finally可以并存)
- finally:是作为异常处理机制的唯一出口
4、一些常见的异常
- 算术异常(除数为0):java.lang.ArithmeticException
- 数字格式转换异常:
java.lang.NumberFoematException - 数组越界异常:
java.lang.ArrayIndexOutOfBoundsException - 空指针异常:java.lang.NullPointerException
- 类转换异常:java.lang.ClassCastException
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try {
System.out.println("请输入第一个数");
String a = sc.next();
int num1 = Integer.parseInt(a);
System.out.println("请输入第二个数");
String b = sc.next();
int num2 = Integer.parseInt(b);
int c = num1 / num2;
System.out.println("结果是:" + c);
Object obj=null;
System.out.println(obj.hashCode());
args=new String[2];
System.out.println(args[3]);
}catch(ArithmeticException e){
System.out.println("算术异常");//若b输入的是0,出现算术异常
}catch(NumberFormatException e){
e.printStackTrace();
System.out.println("数字格式转换异常");//若输入的为非数字字符,出现数字格式转换异常
}catch(ArrayIndexOutOfBoundsException e) {
//所打印的长度超过数组长度出现数组越界异常
e.printStackTrace();
}catch(NullPointerException e){
//没有指定对象就会出现空指针异常
e.printStackTrace();
System.out.println("空指针异常");
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("不管程序是否发生异常,始终执行。");
}
System.out.println("结束");
}
}
边栏推荐
- json-c库的简单使用——将json文件转换为struct.
- [Halcon vision] array
- Learning about opencv (3)
- string null转空字符串(空字符串是什么意思)
- The problem of incomplete or partial display of the last recyclerview is solved
- QRcode二维码(C语言)遇到的问题
- Redis realizes the correct posture of token bucket
- [Halcon vision] Fourier transform of image
- Nacos custom service change subscription
- Controller返回JSON数据
猜你喜欢
随机推荐
[Halcon vision] image filtering
Some descriptions of DS V2 push down in spark
Cause: couldn‘t make a guess for 解决方法
hx711 数据波动大的问题
上传图片获取宽高
PLC overview
Agenda express | list of sub forum agenda on July 27
3.1 leetcode daily question 6
Interview questions and answers for the second company (2)
Learning about opencv (4)
简单化构造函数的继承方法(二)- ES6中的class继承
Perfect / buffer motion framework in sentence parsing JS (for beginners)
[gossip] error loading psychopg2 module: no module named psychopg2
canvas上传图片base64-有裁剪功能-Jcrop.js
Function templates and non template functions with the same name cannot be overloaded (definition of overloads)
卸载魅族应用商店
关于函数模板描述错误的是(链接格式错误怎么解决)
PTA class a 1001
js下载文件,FileSaver.js导出txt、excel文件
【Halcon视觉】算子的结构





![[Halcon vision] morphological expansion](/img/ce/abaca036fce5b67dfe6ac361aecfea.png)
![[Halcon vision] programming logic](/img/1a/b6daac946fbefd8337355dc8b7873e.png)

![[Halcon vision] affine transformation](/img/f1/32284c71e78e6eea390fdb6058ba0f.png)
![[qualcomm][network] QTI service analysis](/img/76/49054ff8c7215eca98cc479ab1d986.png)