C # test Code:
usingSystem;classprogram{Static voidA () {Try{Console.WriteLine ("throwing an exception"); Throw NewException ("did you catch it?"); } finally{Console.WriteLine ("a.finally ()"); } } Static voidB () {Try{C (); Console.WriteLine ("failure! Exception have not been thrown."); } Catch(Exception ex) {Console.WriteLine ("Test b:success! Caught exception!\n"+Ex. ToString ()); } finally{Console.WriteLine ("b.finally ()"); } } Static voidC () {A (); } Static voidD () {Try { Try{Console.WriteLine ("throwing an exception"); Throw NewException ("did you catch it in the right handler?"); } Catch(IndexOutOfRangeException e) {Console.WriteLine ("Test d:failed. Called Wrong handler.\n"+e.tostring ()); } } Catch(Exception ex) {Console.WriteLine ("Test d:success! Caught exception!\n"+Ex. ToString ()); } } Static voidMain () {Console.WriteLine ("Testing A"); Try{A (); Console.WriteLine ("failure! Exception have not been thrown."); } Catch(Exception ex) {Console.WriteLine ("Test a:success! Caught exception!\n"+Ex. ToString ()); } finally{Console.WriteLine ("main.finally ()."); } Console.WriteLine ("\ntesting B"); B (); Console.WriteLine ("\ntesting D"); D (); Console.WriteLine ("\ntesting class Teste"); Teste.run (); Console.WriteLine ("Exiting Test"); } classTeste {Static voidA () {Try{Console.WriteLine ("In A"); B (); } Catch(ArgumentException ae) {Console.WriteLine ("error! Caught argumentexception.\n"+AE. ToString ()); } } Static voidB () {Try{Console.WriteLine ("In B"); C (); } finally{Console.WriteLine ("Leaving B"); } } Static voidC () {Console.WriteLine ("In C"); D (); Console.WriteLine ("error! A () should not being reached in C ()"); A (); } Static voidD () {Console.WriteLine ("in D, throwing ..."); Throw NewException ("Exception Test"); } Public Static voidRun () {Try{A (); } Catch(Exception ex) {Console.WriteLine ("Test c:success! Caught exception!\n"+Ex. ToString ()); } } }}
Throw Exception
Code compilation:
Mcs-nostdlib -r:compile_r_lib/mscorlib.dll-r:compile_r_lib/system.runtime.dll-r:compile_r_lib/ System.Console.dll App/throwexception.cs
Code to run:
Runtime_mac/corerun App/throwexception.exe
Running results when managed exception handling is not implemented:
Testing athrowing an exception{0x7fff7c0e1300-0x108e7c840} ASSERT [DEBUG ] at/git/dotnet/coreclr/src/pal/src/ ARCH/I386/CONTEXT.CPP.38:TRACE/BPT Trap:5
The results of the operation after the initial implementation of managed exception handling:
Testing athrowing an exceptiona.finally () Test a:success! Caught exception! System.Exception:Did you catch it? At PROGRAM.A () at Program.main () main.finally (). Testing bthrowing an exceptiona.finally () Test b:success! Caught exception! System.Exception:Did you catch it? At PROGRAM.A () @ program.b () b.finally () testing dthrowing an exceptiontest d:success! Caught exception! System.Exception:Did you catch it on the right handler? At PROGRAM.D () trace/bpt Trap:5
corresponding git commit: Implement basic support for managed exception handling
The corresponding Git pull request:implement basic support for managed exception handling
CoreCLR on Mac: Experience managed exception handling