Properly handling exceptions Part II (Passing it back)

D

Darrel

I asked this a while ago but got sidetracked and now have finally gotten
back to it.

Karl wrote a nice (and easy to read) article here:

http://codebetter.com/blogs/karlseguin/archive/2006/04/05/142355.aspx

I've given it a once through, and, while it made sense, I know I'm still
missing a large amount of knowelege regarding global error handling.

Anyways, here's my specific scneario:

I have a function I'd like to call that returns a data set:

myFunction()
try
...to connect to DB and grab DS...
...return DS...
catch
...return an error...
end try
end myFunction

I normally do this as 99% of the time (at least when building an app) my
errors are caused by a DB issue. A bad query/bad table permissions/etc.
Typically, if I embed the function on my page, I just do a response.write.
Of course, that won't work if I'm calling the class from elsewhere.

So, based on that, if all I want is to know if the function properly
executed, how can I pass that back? It seems that I can either return a DS
or I can return an exception. Can I/Should I just do something like (pseudo
code):

if myFunction().typeOfReturnValue = ds then
' things worked
else
' things did not work.
end if
 
V

Vlad Iliescu

Wouldn't it be easier to see if the function threw an exception? You
shouldn't catch all types of exceptions, only the ones you expect to
happen (that is, it's better to catch a SqlException than an
Exception).
Also, I think you should catch those exceptions in the UI layer, where
you can treat them properly. This way you'll know if a method was
successful (no exception is thrown) or not (an exception of an expected
type is thrown).
 
D

Darrel

Wouldn't it be easier to see if the function threw an exception? You
shouldn't catch all types of exceptions, only the ones you expect to
happen (that is, it's better to catch a SqlException than an
Exception).

Umm...I dunno. My lack of knowlege about the broader concept of exceptions
is showing here.
Also, I think you should catch those exceptions in the UI layer, where
you can treat them properly. This way you'll know if a method was
successful (no exception is thrown) or not (an exception of an expected
type is thrown).

So, would the method be to wrap my function that calls the function in a
try/catch as well?

try
call my otherFunction()
catch
do something
end try

otherFunction()
try
...to connect to DB and grab data...
catch
throw;
end try
end function

-Darrel
 
V

Vlad Iliescu

I'm thinking something more along the lines of:

try
call my otherFunction()
catch SqlException
do something (warn the user, etc)
end try


otherFunction()
try
...to connect to DB and grab data...
finally
close connection, etc..
end try
end function

And in Main (if you're using windows forms) you could wrap
Application.Run in a try/catch block for Exception and display a
generic error message/log the exception. But that's just me.

The thing to remember would be to (almost)always be very specific about
what types of exceptions you catch, or you might hide bugs (imagine
that your DB access code performs a division by zero, and you're
catching Exception ;) )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top