习惯使用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的异常还是输出了