code_junkie

Performance Considerations for throwing Exceptions

Nov 10th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. try
  2. {
  3. ... // some code
  4. }
  5. catch (Exception ex)
  6. {
  7. ... // Do something
  8. throw new CustomException(ex);
  9. }
  10.  
  11. try
  12. {
  13. ... // some code
  14. }
  15. catch (Exception ex)
  16. {
  17. .. // Do something
  18. throw ex;
  19. }
  20.  
  21. try
  22. {
  23. ... // some code
  24. }
  25. catch (Exception ex)
  26. {
  27. .. // Do something
  28. throw;
  29. }
  30.  
  31. try {
  32. // something that will raise an exception almost half the time
  33. } catch( InsufficientFunds e) {
  34. // Inform the customer is broke
  35. } catch( UnknownAccount e ) {
  36. // Ask for a new account number
  37. }
  38.  
  39. try
  40. {
  41. // some code
  42. }
  43. catch (Exception ex) { throw ex; }
  44.  
  45. try
  46. {
  47. // some code
  48. }
  49. catch (Exception ex) { throw; }
  50.  
  51. public bool Load(string filepath)
  52. {
  53. if (File.Exists(filepath)) //Avoid throwing by checking state
  54. {
  55. //Wrap anyways in case something changes between check and operation
  56. try { .... }
  57. catch (IOException ioFault) { .... }
  58. catch (OtherException otherFault) { .... }
  59. return true; //Inform caller of success
  60. }
  61. else { return false; } //Inform caller of failure due to state
  62. }
Add Comment
Please, Sign In to add comment