首页 > 其他分享 >10.20(异常处理)

10.20(异常处理)

时间:2023-10-20 21:45:35浏览次数:27  
标签:10.20 Exception Level 处理 System println catch 异常 out

package homework;
import javax.swing.*;

class text {
    public static void main(String[] a)
    {
        int i=1, j=0, k;
        k=i/j;


        try
        {

            k = i/j;    // Causes division-by-zero exception
            //throw new Exception("Hello.Exception!");
        }

        catch ( ArithmeticException e)
        {
            System.out.println("算式错误"+ e.getMessage());
        }

        catch (Exception e)
        {
            if (e instanceof ArithmeticException)
                System.out.println("算式错误");
            else
            {
                System.out.println(e.getMessage());

            }
        }


        finally
        {
            JOptionPane.showConfirmDialog(null,"OK");
        }

    }
}

 多层异常捕获,try-catch像if一样可以嵌套使用,catch是优先原则,先捕捉到异常后就停止不会再进行后续的catch判断。

public class CatchWho2 { 
    public static void main(String[] args) { 
        try {
                try { 
                    throw new ArrayIndexOutOfBoundsException(); 
                } 
                catch(ArithmeticException e) { 
                    System.out.println( "ArrayIndexOutOfBoundsException" + "try-catch"); 
                }
            throw new ArithmeticException(); 
        } 
        catch(ArithmeticException e) { 
            System.out.println("ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) { 
            System.out.println( "ArrayIndexOutOfBoundsException" + "/try-catch"); 
        } 
    } 
}

 

package homework;

public class text {


    public static void main(String args[]) {

        int result;

        try {

            System.out.println("in Level 1");


            try {

                System.out.println("in Level 2");
                // result=100/0;  //Level 2

                try {

                    System.out.println("in Level 3");

                    result=100/0;  //Level 3

                }

                catch (Exception e) {

                    System.out.println("Level 3:" + e.getClass().toString());

                }


                finally {

                    System.out.println("In Level 3 finally");

                }


                // result=100/0;  //Level 2


            }

            catch (Exception e) {

                System.out.println("Level 2:" + e.getClass().toString());

            }
            finally {

                System.out.println("In Level 2 finally");

            }

            // result = 100 / 0;  //level 1

        }

        catch (Exception e) {

            System.out.println("Level 1:" + e.getClass().toString());

        }

        finally {

.             System.out.println("In Level 1 finally");

        }

    }

}

 

在Level 中,没有发生任何异常,因此不执行任何操作。

在Level 2中,发生了一个除以零的异常,该异常被第二个catch块捕获并处理。在catch块中,输出了异常的类型。无论是否发生异常,finally块都会执行,输出"In Level 2 finally"。

在Level 3中,同样发生了一个除以零的异常。这个异常被第三个catch块捕获并处理。在catch块中,输出了异常的类型。无论是否发生异常,finally块都会执行,输出"In Level 3 finally"。

 当发生异常时,程序会按照从内到外的顺序执行相应的catch块,并在最后执行finally块。

package homework;


public class text {


    public static void main(String[] args)
    {

        try{


            System.out.println("in main");

            throw new Exception("Exception is thrown in main");

            //System.exit(0);


        }

        catch(Exception e)

        {

            System.out.println(e.getMessage());

            System.exit(0);

        }

        finally

        {

            System.out.println("in finally");

        }

    }


}

 由于System.exit(0)的存在,程序会在抛出异常后立即终止,不会继续执行后面的代码。因此,"in finally"这一行代码不会被输出。

追踪异常的传播路径

package homework;


public class text {
    public static void main( String args[] )
    {
        try {
            method1();
        }
        catch ( Exception e ) {
            System.err.println( e.getMessage() + "\n" );
            e.printStackTrace();
        }
    }

    public static void method1() throws Exception
    {
        method2();
    }

    public static void method2() throws Exception
    {
        method3();
    }

    public static void method3() throws Exception
    {
        throw new Exception( "Exception thrown in method3" );
    }
}

 

 

main方法中,调用了method1方法。如果method1抛出异常,catch块将捕获该异常并打印异常信息和堆栈跟踪。

method1方法调用method2方法,method2方法调用method3方法。method3方法直接抛出一个带有消息"Exception thrown in method3"的异常。

package homework;


import java.io.*;
public class text {
    public static void main(String[] args)
    {
        try {
            throwsTest();
        }
        catch(IOException e) {
            System.out.println("主函数异常");
        }
    }

    private static void throwsTest()  throws ArithmeticException,IOException {
        System.out.println("函数异常");
        throw new IOException();
        //throw new ArithmeticException();
    }
}

当一个方法声明抛出多个异常时,在此方法调用语句处只要catch其中任何一个异常,代码就可以正常运行

 

标签:10.20,Exception,Level,处理,System,println,catch,异常,out
From: https://www.cnblogs.com/xuxingkai/p/17778049.html

相关文章

  • SpringMVC自定义处理返回值demo和异步处理模式DeferredResult demo
    搭建自定义返回值处理器demo新建springboot项目修改pom.xml<!--新增依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><gro......
  • 常用的异常处理情况
    经过我这几天对异常处理的资料的搜集,我发现理解和处理异常对于任何一个Java开发人员来说都是至关重要的。因为在Java项目中,异常处理是确保程序的稳定性和可靠性的关键一步。这篇报告,我总结了一下在Java项目中常见的异常情况以及它们的处理方法。从他们的基本概念开始,然后深入一......
  • 关于tomcat容器抛出的异常解决方案之一
    1,描述问题类型:调试信息泄露如:现要求:前端页面不显示调试信息。解决方案:替换默认的tomcat<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><e......
  • 10.20每日总结
      例题展示例题解决......
  • 2023.10.20——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.休息明日计划:学习......
  • 性能测试中数据处理经验分享
    在性能测试中,有一个无法避免的问题,就是如何处理性能测试用例使用到的数据,其中包括前置数据、运行时数据和后置脏数据清理。相信大家在做性能测试中也会遇到跟我相同的问题,我分享一下自己的思路和解决方案,仅供参考。仅仅根据个人经验,我将性能测试中数据处理分成以下几种类型。静态......
  • 【玩转 Node 连载 1/6】我是如何在 Node.js 中定位异常的
    第72届早早聊大会将于2023年10月29日(下周日)举办-前端跨端方案|跨端同构,方法框架,5位讲师下午直播,关键词:跨端框架/跨端组件库/小程序/Harmony/Electron。跟早早聊一起,码上多平台,上车链接:https://www.zaozao.run/conf/c72本文是2023年4月8日,第六十二届-前端早早......
  • QPQ表面处理,中国工程物理研究院到公司交流考察
    成都工具研究所有限公司的前身是成都工具研究所,于1956年创建于北京,是原机械工业部的直属研究所,是我国机械工业的综合性工具科研机构。公司官网:http://www.ctri.com.cn/公司主要从事精密切削工具、精密测量仪器以及表面改性处理技术的技术研究、产品开发和应用服务。7月2日上午,中......
  • Java拾贝第七天——异常
    Java拾贝不建议作为0基础学习,都是本人想到什么写什么在程序实际运行过程中,可能存在大量的未知问题,所以在程序中对错误的处理是极其重要的。Java提供的异常处理机制可以更好地解决这方面的问题。认识异常publicstaticvoidmain(String[]args){System.out.prin......
  • 2D物理引擎 Box2D for javascript Games 第五章 碰撞处理
    2D物理引擎Box2DforjavascriptGames第五章碰撞处理碰撞处理考虑到Box2D世界和在世界中移动的刚体之间迟早会发生碰撞。而物理游戏的大多数功能则依赖于碰撞。在愤怒的小鸟中,小鸟摧毁小猪的城堡时,便是依赖碰撞而实现的;在图腾破坏者中,当神像坠落到图腾上或摔碎在地面上......