zoukankan      html  css  js  c++  java
  • JAVA异常处理

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

            

    }

        

    }

    }

    注意:

    1.当有多个嵌套的trycatchfinally时,要特别注意finally的执行时机。

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

    public class SystemExitAndFinally 

    {

        public static void main(String[] args)

        {

            try

    {

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

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

            }

            catch(Exception e)

            {

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

                System.exit(0);

            }

            finally

            {

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

            }

        }

    }

    没有 System.exit(0);的时候会输出in finally要是有System.exit(0);则会退出系统不执行finally语句。

     

     

    // UsingExceptions.java

    // Demonstrating the getMessage and printStackTrace

    // methods inherited into all exception classes.

    public class PrintExceptionStack {

       public static void main( String args[] )

       {

          try {

             method1();

          }

          catch ( Exception e ) {

             System.err.println( e.getMessage() + " " );

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

       }

    }

     

    应该注意:

    当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。

    可使用printStackTrace 和 getMessage方法了解异常发生的情况:

    1.printStackTrace:打印方法调用堆栈。

    2.每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

  • 相关阅读:
    python+selenium webdriver.firefox()方式配置浏览器设置
    spring框架学习之依赖注入(二)
    spring框架体系结构介绍
    mongoDB数据更新与操作符
    mongoDB学习笔记<一>
    关于IOS AFNetWorking内存泄漏的问题
    IOS 11 下适配UITableView
    调试Xamarin.Android时出现缺少"Mono.Posix 2.0.0"的错误
    EF6 MySQL错误之“Specified key was too long; max key length is 767 bytes”
    Webstrom安装+激活
  • 原文地址:https://www.cnblogs.com/amiee/p/4965967.html
Copyright © 2011-2022 走看看