File tree 2 files changed +42
-1
lines changed
2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
3
-
4
3
namespace Python . Test
5
4
{
6
5
//========================================================================
@@ -55,6 +54,25 @@ public static bool ThrowException()
55
54
{
56
55
throw new OverflowException ( "error" ) ;
57
56
}
57
+
58
+ public static void ThrowChainedExceptions ( )
59
+ {
60
+ try
61
+ {
62
+ try
63
+ {
64
+ throw new Exception ( "Innermost exception" ) ;
65
+ }
66
+ catch ( Exception exc )
67
+ {
68
+ throw new Exception ( "Inner exception" , exc ) ;
69
+ }
70
+ }
71
+ catch ( Exception exc2 )
72
+ {
73
+ throw new Exception ( "Outer exception" , exc2 ) ;
74
+ }
75
+ }
58
76
}
59
77
60
78
Original file line number Diff line number Diff line change @@ -345,6 +345,29 @@ def testPicklingExceptions(self):
345
345
346
346
self .assertEqual (exc .args , loaded .args )
347
347
348
+ @unittest .skipIf (six .PY2 ,
349
+ "exception chaining is only supported in Python 3" )
350
+ def testChainedExceptions (self ):
351
+ if six .PY3 :
352
+ from Python .Test import ExceptionTest
353
+
354
+ try :
355
+ ExceptionTest .ThrowChainedExceptions ()
356
+ except Exception as exc :
357
+ msgs = [
358
+ "Outer exception" ,
359
+ "Inner exception" ,
360
+ "Innermost exception"
361
+ ]
362
+
363
+ for msg in msgs :
364
+ self .assertEqual (exc .Message , msg )
365
+ self .assertEqual (exc .__cause__ , exc .InnerException )
366
+ exc = exc .__cause__
367
+
368
+ else :
369
+ self .fail ("Test should raise an exception" )
370
+
348
371
349
372
def test_suite ():
350
373
return unittest .makeSuite (ExceptionTests )
You can’t perform that action at this time.
0 commit comments