Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Your site has saved me hours of work that I cannot begin to express my satisfaction..."

Geography

Where in the world do Tek-Tips members come from?
TomSlayton (Programmer)
16 Jun 12 9:11
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







jebenson (TechnicalUser)
17 Jun 12 14:01

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

Public Function ProductCost() AS Decimal

Try

Return DataSvc.GetSomeValue()

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

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:

http://support.microsoft.com/kb/315965

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 !

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close