zoukankan      html  css  js  c++  java
  • python异常链

    习惯使用java开发,在java开发里有异常链概念和重新抛出异常,在python是怎么实现的呢?

    1.异常链

    1.1.java实现

        public static void test1() throws Exception{
            throw new Exception("test1 ex");
        }
        
        public static void test2() throws Exception{
            try {
                test1();
            } catch (Exception e) {
                throw new Exception("test2 ex",e);
            }
        }
        
        public static void main(String[] args) throws Exception {
            test2();
        }

    异常输出:

    Exception in thread "main" java.lang.Exception: test2 ex
        at com.epgis.gisserver.EText.test2(EText.java:13)
        at com.epgis.gisserver.EText.main(EText.java:18)
    Caused by: java.lang.Exception: test1 ex
        at com.epgis.gisserver.EText.test1(EText.java:6)
        at com.epgis.gisserver.EText.test2(EText.java:11)
        ... 1 more

    1.2.python实现

    import traceback
    def test1():
        raise Exception("ex test1")
    def test2():
        try:
            test1()
        except Exception as e:
            raise Exception("ex test2") from e
    if __name__ == '__main__':
        test2()

    异常输出:

    Traceback (most recent call last):
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 6, in test2
        test1()
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 3, in test1
        raise Exception("ex test1")
    Exception: ex test1
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 10, in <module>
        test2()
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 8, in test2
        raise Exception("ex test2") from e
    Exception: ex test2

    2.重新抛出异常

    2.1.java实现

    	public static void test1() throws Exception{
    		throw new Exception("test1 ex");
    	}
    	
    	public static void test2() throws Exception{
    		try {
    			test1();
    		} catch (Exception e) {
    			throw new Exception("test2 ex");
    		}
    	}
    	
    	public static void main(String[] args) throws Exception {
    		test2();
    	}

    异常输出:

    Exception in thread "main" java.lang.Exception: test2 ex
        at com.epgis.gisserver.EText.test2(EText.java:13)
        at com.epgis.gisserver.EText.main(EText.java:18)

    可以看出已经将test1的异常给抛弃

    2.2.python实现

    import traceback
    def test1():
        raise Exception("ex test1")
    def test2():
        try:
            test1()
        except Exception as e:
            raise Exception("ex test2") from None
    if __name__ == '__main__':
        test2()

    异常输出:

    Traceback (most recent call last):
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 10, in <module>
        test2()
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 8, in test2
        raise Exception("ex test2") from None
    Exception: ex test2

    与java不同的地方一定要from None

    如果不使用from会怎么样?

    import traceback
    def test1():
        raise Exception("ex test1")
    def test2():
        try:
            test1()
        except Exception as e:
            raise Exception("ex test2")
    if __name__ == '__main__':
        test2()

    异常输出:

    Traceback (most recent call last):
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 6, in test2
        test1()
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 3, in test1
        raise Exception("ex test1")
    Exception: ex test1
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 10, in <module>
        test2()
      File "F:/MyProgram/pythonProgram/Nike/ex_test.py", line 8, in test2
        raise Exception("ex test2")
    Exception: ex test2

    test1的异常还是输出了

  • 相关阅读:
    Script:List NLS Parameters and Timezone
    Script:List Buffer Cache Details
    Know about RAC Clusterware Process OPROCD
    RAC Deadlock For Example
    Know more about redo log buffer and latches
    如何设计分区索引
    SCN may jump in a distributed transaction with dblink
    Script to Collect Log File Sync Diagnostic Information (lfsdiag.sql)
    Oracle学习笔记:10046 SQL tracle event
    Oracle学习笔记:创建physical standby
  • 原文地址:https://www.cnblogs.com/SmilingEye/p/11358122.html
Copyright © 2011-2022 走看看