Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

n-Tier Error Handling (Newbie Question)

Status
Not open for further replies.

TomSlayton

Programmer
Oct 21, 2011
19
US
Hi,

I am kind of new to vb.net development and am creating an n-tier application. I am trying get my error handling setup properly between the tiers. The tiers are logically separated w/in the application. In this example let’s say I am trying to return a decimal value to the view from a query against the database. What is the best way to return a message back through the tiers to the end user if there is an error?


Data Tier – Talks to the datasource

Some class has this function:
--------------------------------
Public Shared Function GetSomeValue() As Decimal
Try
X = SELECT Sum (Foo) FROM Bar
Return x
Catch
Throw
EndTry
End Function


Business Objects –

Some business class has this:
------------------------------------
Public Function ProductCost() AS Decimal
Try
Return DataSvc.GetSomeValue()
Catch
Throw
EndTry

End Function


View
----------------
Let's say I call the BizObj.ProductCost() method and an error occurs in the data tier or business logic tier. The function is setup to return decimal how can I return a string to the view with a friendly end-user message?

Thanks,

Tom







 
Set up the Try...catch code like this:

Public Function ProductCost() AS Decimal

Try

Return DataSvc.GetSomeValue()

Catch [red]ex As Exception[/red]

'handle the exception here, such as writing to a log file.
'do it here so you can know where the error actually occurred.

WriteErrorToLogFile(ex) 'note: you will need to write this sub

Throw [red]ex[/red] 'error gets raised to code that called this function

End Try

End Function


Calling Code:

Try

Dim p As Decimal = ProductCost()

Catch ex As Exception

'handle the exception here, such as writing to a log file.
'do it here so you can know where the error actually occurred.

WriteErrorToLogFile(ex) 'note: you will need to write this sub

Throw ex 'error gets raised to code that called this function

End Try

Just keep throwing the error until it gets to the original code that started the calls.

You can define your own Exceptions to throw, if you want to make the error messages more user-friendly.

Instead of this: Throw ex, you can do this:

Dim exc As Exception

If ex.Message = "Foo" Then
exc = New Exception("Red Alert!")
ElseIf ex.Message = "Bar" Then
exc = New Exception("Arrr, batten down the hatches, me hearties!")
EndIf

Throw exc

Check out this link for more:




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top