Guest User

The mystery of the twin exceptions

a guest
Feb 27th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. try { /* init code here */ }
  2. catch (Exception ex)
  3. {
  4. try
  5. {
  6. _DeinitializeEngine();
  7. }
  8. catch (Exception ex2)
  9. {
  10. throw new OCRException("Engine failed to initialize; ALSO failed to deinitialize engine!", ex2);
  11. }
  12. finally
  13. {
  14. throw new OCRException("Engine failed to initialize; failed to initialize license!", ex);
  15. }
  16. }
  17.  
  18. static void Main(string[] args)
  19. {
  20. try
  21. {
  22. principalMethod();
  23. }
  24. catch (Exception e)
  25. {
  26. Console.WriteLine("Test : " + e.Message);
  27. }
  28. Console.Read();
  29. }
  30.  
  31. public static void principalMethod()
  32. {
  33. try
  34. {
  35. throw new Exception("Primary");
  36. }
  37. catch (Exception ex1)
  38. {
  39. try
  40. {
  41. methodThatCanCrash();
  42. }
  43. catch
  44. {
  45. throw new Exception("Cannot deinitialize", ex1);
  46. }
  47. }
  48. }
  49.  
  50. private static void methodThatCanCrash()
  51. {
  52. throw new NotImplementedException();
  53. }
  54.  
  55. try { /* init code here */ }
  56. catch (Exception ex)
  57. {
  58. // Passing original exception as inner exception
  59. Exception ocrex = new OCRException("Engine failed to initialize", ex);
  60.  
  61. try
  62. {
  63. _DeinitializeEngine();
  64. }
  65. catch (Exception ex2)
  66. {
  67. // Passing initialization failure as inner exception
  68. ocrex = new OCRException("Failed to deinitialize engine!", ocrex);
  69. }
  70. throw ocrex;
  71. }
Add Comment
Please, Sign In to add comment