zoukankan      html  css  js  c++  java
  • 动手动脑实验-异常

    1.

    package seven_zhoulianxiti;

    public class ChachWho {
    public static void main(String[] args) {
    try {
    try {
    throw new ArrayIndexOutOfBoundsException();
    }
    catch(ArrayIndexOutOfBoundsException 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");
    }
    }

    }

    运行结果:

     2.运行以下代码

    package seven_zhoulianxiti;

    public class CatchWho2 {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    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");
    }

    }

    }

    运行结果截图:

     3多个try catch语句嵌套:

    public class EmbededFinally {


    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");

    }

    }

    }

    运行截图:

    总结:

    当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

    4.finally语句块一定会执行。

    package seven_zhoulianxiti;

    public class SystemExitAndFinally {
    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");

    }

    }
    }

    在Java语言的异常处理中,finally块的作用是为了保证不管出现什么情况,finally块里的代码一定会被执行。由于程序执行return就意味着结束对当前函数的调用并跳出这个函数体,因此任何语句要执行都只能在return前执行(当遇到exit是,会直接退出程序不会执行finally块)。所以finally语句块都是会被执行的,除了遇到exit.

    package seven_zhoulianxiti;

    public class SystemExitAndFinally {
    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");

    }

    }
    }

     
  • 相关阅读:
    TP框架基础
    PHP文件上传
    layui-简单的登录注册界面
    layui-注册界面
    JavaScript原生Ajax请求纯文本数据
    ajax异步请求数据
    PHP数据库连接
    PHP页面跳转以及伪登录实例
    OVER 分析函数
    Ubuntu 16 vi输入方向键会变成ABCD的解决方法
  • 原文地址:https://www.cnblogs.com/mxk123456/p/11789279.html
Copyright © 2011-2022 走看看