/**
* @ClassName: Que
* @Description: 自行编写程序,验证try/catch/finally的用法,验证数学异常、空指针异常、数字格式异常、索引越界异常、类型转换异常。
* @Author: Wanglt
* @CreateDate: 2020年2月29日
*
*/
public class Que {
public static void main(String[] args) {
try {
// 数学异常
int a = 5 / 0;
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
// 空指针
int[] nullArr = null;
System.out.println(nullArr[0]);
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
// 数字格式异常
System.out.println(Integer.parseInt("love"));
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
// 索引越界异常
int a[] = {1,2,3};
System.out.println(a[5]);
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
// 类型转换异常
int num = 12;
Object object = (Object)num;
System.out.println((String)object);
} catch (Exception e) {
System.out.println(e.toString());
} finally {
System.out.println("结束");
}
}
}
}
}
}
}
try/catch/finally的用法
最新推荐文章于 2024-02-02 10:49:19 发布