ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day16异常1

2022-05-13 23:00:53  阅读:169  来源: 互联网

标签:void System println Day16 catch 异常 public out


package com.exception.demo01;

public class demo01 {
public static void main(String[] args) {
try{new demo01().a();}//StackOverflowError异常
catch (Throwable e){
System.out.println("程序出现异常");
}
//System.out.println(11/0);//ArithmeticException异常
}
public void a(){
b();
}
public void b(){
a();
}
}



package com.exception.demo01;

public class Test {
public static void main(String[] args) {
int a=1;
int b=0;
//假设要捕获多个异常,从小到大
try {
new Test().test(1,0);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
public void a(){
b();
}
public void b(){
a();
}
//假设这个给方法中处理不了这个给异常,方法上抛出异常 throws
public void test(int a,int b) throws ArithmeticException {
if(b==0){
throw new ArithmeticException();//主动抛出异常 throw 一般在方法中使用
}
System.out.println(a/b);
}

}
/*
假设要捕获多个异常,从小到大
try {//try是监控区域
new Test().a();

} catch (Error e){//catch(想要捕获的异常类型)捕获异常
System.out.println("程序1出现异常被除数不能为0");}
/*catch (ArithmeticException e){//catch(想要捕获的异常类型)捕获异常
System.out.println("程序出现异常被除数不能为0");}


catch (Exception e){
System.out.println("程序2出现异常被除数不能为0");
}
catch (Throwable t){
System.out.println("程序3出现异常被除数不能为0");
}
finally {//处理善后工作
System.out.println("finally");
}
//finally可以不要 finally,假设IO,资源,关闭
*/


package com.exception.demo01;

public class Test2 {
public static void main(String[] args) {
int a=1;
int b=0;
//Ctrl+Alt+T 生成try carch的快捷键
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();//打印错误的栈信息
} finally {
}

}
}




标签:void,System,println,Day16,catch,异常,public,out
来源: https://www.cnblogs.com/tomn/p/16268611.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有