473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Properly handling exceptions Part II (Passing it back)

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/karlsegu...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
Apr 21 '06 #1
3 1020
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).

Apr 21 '06 #2
> 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
Apr 21 '06 #3
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 ;) )

Apr 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
6
by: Jesper Ordrup Christensen | last post by:
Say I've created a piece of code that involves both sql, io and some number conversions. Being a responsible developer I have tried to catch all of the exceptions - but how can I be sure? Is...
34
by: rawCoder | last post by:
I have read that Exception Handling is expensive performance wise ( other than Throw ) , but exactly how ? Please consider the following example ... ////////////////// Code Block 1...
5
by: | last post by:
Unfortunately I've used exception handling as a debugging tool. Now I want to be smarter about handling errors. Today in the global.asx in the Application_OnError event, I inserted code to email...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.