首页 > 编程语言 >Java笔记(10)——异常处理

Java笔记(10)——异常处理

时间:2022-12-19 19:37:38浏览次数:46  
标签:10 Java int System 笔记 println catch 异常 out


1、Java异常

  Java运行时发生异常可以分为两类:

  1. ​Error​​​:​​JVM​​系统内部错误、资源耗尽等问题产生的异常。
  2. ​Exception​​:编程错误或偶然的外在因素导致的。

2、常见的异常

2.1 RuntimeException运行异常

  • 错误的类型转换。
  • 数组下标越界。
  • 空指针访问。

2.2 IOException

3、异常捕获

3.1

  如果知道​​try​​​中是何种异常,可以用该类异常作为​​catch​​​中的参数(例如:​​ArithmeticException​​​),也可以使用其父类(例如:​​RuntimeException​​​)作为其​​catch​​​中的参数。但使用与该类无关的异常,​​catch​​将不会执行。

try{
//程序代码
}catch(ExceptionName1 e){
// Catch块
}catch(ExceptionName2 e){
// Catch块
}finally {
// 无条件执行的语句
}

举例:

try {
// 放置可能会出现异常的代码
int j = 10 / 0;
}
// 在try的后面可以有多个catch
catch (ArithmeticException exception){
System.out.println("0. " + exception.getMessage());
} catch (NullPointerException ex){
System.out.println("1. " + ex.getMessage());
}catch (ClassCastException e){
System.out.println("2. " + e.getMessage());
}
// 可以抓住所有catch括号中的异常的子类对象
// 范围大的异常(异常类的父类)要放在异常类的子类的后面
// 放在前面出错
catch (RuntimeException e){
System.out.println("3. " + e.getMessage());
}

System.out.println("end....");

/*
0. / by zero
end....
*/

3.2

  调用异常对象的成员变量的方法:

  • ​getMessage()​​:得到有关异常的信息。
  • ​printStackTrace()​​:用来跟踪异常事件发生时执行堆栈的内容。
public static void main(String[] args) {
int[] a = new int[]{1, 2, 3, 4};
try {
for (int i = 0; i < 6; i++){
System.out.println(a[i]);
}
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组下标越界异常:" + e.getMessage()); // 得到有关异常的信息
//e.printStackTrace();
}finally {
System.out.println("数组下标可能越界。。。");
}
}
/*
1
2
3
4
数组下标越界异常:4
数组下标可能越界。。。
*/

上述异常,在不捕获的前提下,Java能够自己捕获,并且编译通过。

下面的必须捕获,否则报错。

4、IOException异常捕获

自定义异常

编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除。
对缺少命令行参数(ArrayIndexOutOfBoundsException)、
除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
提示:
(1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能。
(2)在main()方法中使用异常处理语句进行异常处理。
(3)在程序中,自定义对应输入负数的异常类(EcDef)。
(4)运行时接受参数 java EcmDef 20 10
//args[0]=“20” args[1]=“10”
(5)Interger类的static方法parseInt(String s)将s转换成对应的int值。如int a=Interger.parseInt(“314”); //a=314;

// 需要继承检查时异常 Exception 或 运行时异常 RuntimeException

public class EcDef extends RuntimeException {
private static final long serialVersionUID = 1L;

public EcDef(){

}

public EcDef(String msg){
super(msg);
}
}
public class EcmDef {
public static void main(String[] args) {
//快捷键 ctrl + Alt + T 添加try catch 捕获异常
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int ret = ecm(i ,j);
System.out.println("result = " + ret);
} catch (NumberFormatException e) { // 参数转换异常
System.out.println("输入的参数不能转换为INT型。。。");
e.printStackTrace();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("输入的参数个数不足。。。");
}catch (ArithmeticException e){
System.out.println("除数不能为0。。。");
}catch (EcDef e){
System.out.println("运算的数不能为负数。。");
e.printStackTrace();
}
}
public static int ecm(int i, int j){
if(i < 0 || j < 0){
throw new EcDef("不能输入负数");
}
return i / j;
}
}


标签:10,Java,int,System,笔记,println,catch,异常,out
From: https://blog.51cto.com/u_15917702/5953710

相关文章