2

I have a question about how to properly deal with errors. I am working on a three tiered application. If an error is created on the data tier, I would like to pass the error to the business tier and process it there. What is the best method to accomplish this? I am using .net 2.0 and visual studio 2005.

Thanks for any advice jason

jason
  • 3,821
  • 10
  • 63
  • 120

1 Answers1

2

Use a Try...Catch in your business-layer with your calls to your data-layer within the Try.

Try
  'call data-layer
Catch ex As Exception
  'deal with exception / log
End Try

If you still want to use Try...Catch in your data-layer then you need to Throw (to preserve stacktrace) or Throw ex within the Catch, otherwise don't use Try...Catch in your data-layer at all.

Try
  data = dataLayer.GetData()
Catch ex As Exception
  Throw
End Try
alundy
  • 857
  • 8
  • 22
  • 3
    Note: use `Throw` instead of `Throw ex` to keep the stacktrace. http://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex Otherwise you wouldn't see that the exception actually was raised in `DoWork` or even in "deeper" methods called from `DoWork`. – Tim Schmelter Feb 27 '12 at 12:42
  • Changed my answer to reflect this advice. Thanks. – alundy Feb 27 '12 at 12:52
  • Thanks! I will implement this – jason Feb 27 '12 at 12:53