zoukankan      html  css  js  c++  java
  • AVOID "throw e" !!!

    There three kind of catch ... throw method:

    catch(Exception e)
                
    {
                    
    throw e;
                }

                
    catch(Exception)
                
    {
                    
    throw;
                }

                
    catch
                
    {
                    
    throw;
                }

    The last two method are the same, while the first is different.

    The first method will swallow down the exception's StackTrace

    Example:

     1class Class1
     2    {
     3        [STAThread]
     4        static void Main(string[] args)
     5        {
     6            try
     7            {
     8                Class1 app = new Class1();
     9                app.fun();
    10            }

    11            catch (System.Exception e)
    12            {
    13                Console.WriteLine(e.StackTrace);
    14            }

    15            Console.ReadLine();
    16        }

    17        
    18        void fun()
    19        {
    20            try
    21            {
    22                throwE();
    23            }

    24            catch(Exception)
    25            {
    26                throw;
    27            }

    28        }

    29        void throwE()
    30        {
    31            throw new Exception("my Exception");
    32        }

    33    }

    the result is:

    at ConsoleApplication1.Class1.throwE()
    at ConsoleApplication1.Class1.fun()
    at ConsoleApplication1.Class1.Main(String[] args)

    if we change the fun() to :

     1        void fun()
     2        {
     3            try
     4            {
     5                throwE();
     6            }

     7            catch(Exception e)
     8            {
     9                throw e;
    10            }

    11        }

    the result is:

    at ConsoleApplication1.Class1.fun()
    at ConsoleApplication1.Class1.Main(String[] args)

    we lose the true throw exception trace, so avoid "throw e"

  • 相关阅读:
    链表操作二——中间结点的删除等
    stack vector queue 等的实现方式<<0922
    任何和日期相关的函数都在这里<<0922
    类函数返回该类的问题<<0922
    Android学习笔记之PullToRefreshListView和BaseAdapter的使用
    记录Android学习过程中遇到的问题
    ruby appium 准备环境
    os x升级到10.10后appium不能测试通过的解决办法
    appium 在ios模拟器上面成功运行
    appium IOS真机测试
  • 原文地址:https://www.cnblogs.com/skyfei/p/516406.html
Copyright © 2011-2022 走看看