Day16异常1

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 {        }    }}

Original: https://www.cnblogs.com/tomn/p/16268611.html
Author: 1号小白学编程
Title: Day16异常1

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/569353/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球