473,770 Members | 7,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling exceptions

Hello

I'm a bit confused on how one should handle exceptions. I'm currently
building an ADO.NET Windows Forms application. I've built a class for
manipulating data within the database in question. This class is used
from forms. Let's assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to
show when giving an error message?

Is there any "centralize d" way of handling exceptions: for example,
having a centralized exception handler catch everything, and show an
error message relative to the caught exception?

Thanks
Aug 9 '08 #1
37 1907
Ok this is a multi answer question because there is no a right way to handle
all the exceptions, one that works for me is:
First if the Exception is an expected exception handle that near the code
that gives the Exception.
Second if you can recover from the exception do it near when it's throwing.
If you don't know or expect the exception and because you are using windows
form you can set a Global exception handler in the application object like
this:
Application.Set UnhandledExcept ionMode(Unhandl edExceptionMode .Automatic);
Application.Thr eadException += new
System.Threadin g.ThreadExcepti onEventHandler( Application_Thr eadException);
static void Application_Thr eadException(ob ject sender,
System.Threadin g.ThreadExcepti onEventArgs e)
{
//Handle here an Unhandled Exception
}

And there you will handle an unexpected exception, but there is a catch, you
need to know if the application can continue after the exception or not.

Regards,

Bela Istok
"Sweetiecak es" <x@x.comwrote in message
news:48******** *************** @news.fv.fi...
Hello

I'm a bit confused on how one should handle exceptions. I'm currently
building an ADO.NET Windows Forms application. I've built a class for
manipulating data within the database in question. This class is used from
forms. Let's assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to show
when giving an error message?

Is there any "centralize d" way of handling exceptions: for example, having
a centralized exception handler catch everything, and show an error
message relative to the caught exception?

Thanks
Aug 9 '08 #2
SweetieCakes,

A very retro question you ask, I think from about the 70's.

But you can of course make a method that handles those in a kind of general
way and pass the exception.

I don't know if it was just speaking words, but an SQL exception should only
be in design time, because it catches the errors you have made in your
scripts.
Cor

"Sweetiecak es" <x@x.comschre ef in bericht
news:48******** *************** @news.fv.fi...
Hello

I'm a bit confused on how one should handle exceptions. I'm currently
building an ADO.NET Windows Forms application. I've built a class for
manipulating data within the database in question. This class is used from
forms. Let's assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to show
when giving an error message?

Is there any "centralize d" way of handling exceptions: for example, having
a centralized exception handler catch everything, and show an error
message relative to the caught exception?

Thanks
Aug 9 '08 #3
Cor Ligthert[MVP] wrote:
I don't know if it was just speaking words, but an SQL exception should
only be in design time, because it catches the errors you have made in
your scripts.
I'm in your server room *right now*, unplugging your network. Let's see how
your users like cryptic error messages. :-)

--
J.
Aug 9 '08 #4
Cor Ligthert[MVP] <no************ @planet.nlwrote :
A very retro question you ask, I think from about the 70's.

But you can of course make a method that handles those in a kind of general
way and pass the exception.

I don't know if it was just speaking words, but an SQL exception should only
be in design time, because it catches the errors you have made in your
scripts.
And of course nothing *ever* goes wrong with valid SQL... The database
never goes down, there are never concurrency violations, locking
problems, failing to find expected data etc.

There are plenty of reasons why you may see SQL exceptions at execution
time.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 9 '08 #5
Let's not shoot him about this message.

Regards,

Bela Istok
"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:24******** *************** ***********@mic rosoft.com...
SweetieCakes,

A very retro question you ask, I think from about the 70's.

But you can of course make a method that handles those in a kind of
general way and pass the exception.

I don't know if it was just speaking words, but an SQL exception should
only be in design time, because it catches the errors you have made in
your scripts.
Cor

"Sweetiecak es" <x@x.comschre ef in bericht
news:48******** *************** @news.fv.fi...
>Hello

I'm a bit confused on how one should handle exceptions. I'm currently
building an ADO.NET Windows Forms application. I've built a class for
manipulating data within the database in question. This class is used
from forms. Let's assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to show
when giving an error message?

Is there any "centralize d" way of handling exceptions: for example,
having a centralized exception handler catch everything, and show an
error message relative to the caught exception?

Thanks
Aug 9 '08 #6

Check this entry out:
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx


"Sweetiecak es" <x@x.comwrote in message
news:48******** *************** @news.fv.fi...
Hello

I'm a bit confused on how one should handle exceptions. I'm currently
building an ADO.NET Windows Forms application. I've built a class for
manipulating data within the database in question. This class is used from
forms. Let's assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to show
when giving an error message?

Is there any "centralize d" way of handling exceptions: for example, having
a centralized exception handler catch everything, and show an error
message relative to the caught exception?

Thanks

Aug 10 '08 #7
Nice post sloan, but this is the opposite site of the question. The Cwalina
post talks about throwing exceptions not handling the exceptions. But it's
good to know that too.

Regards,

Bela Istok
"sloan" <sl***@ipass.ne twrote in message
news:OR******** ********@TK2MSF TNGP04.phx.gbl. ..
>>
Check this entry out:
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx


"Sweetiecake s" <x@x.comwrote in message
news:48******* *************** *@news.fv.fi...
>>Hello

I'm a bit confused on how one should handle exceptions. I'm currently
building an ADO.NET Windows Forms application. I've built a class for
manipulatin g data within the database in question. This class is used
from forms. Let's assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to
show when giving an error message?

Is there any "centralize d" way of handling exceptions: for example,
having a centralized exception handler catch everything, and show an
error message relative to the caught exception?

Thanks

Aug 10 '08 #8

Most times, you will write

try
{}
finally
{}

blocks. notice no "catch". You don't catch them.

Then, in your presentation layer, you can catch them.

try
{

}
catch(Exception ex)
{
MessageBox.Show (ex.Message);
//Response.Write( ex.Message); //asp.net

}
There's one more tactic. When you want to catch a dotNet, un-user friendly
exception, and make it into something nicer.

This might be in the BAL layer.

try
{
//try something against the db ........
}
catch ( SqlException sqlex )
{
throw new MyCustomDatabas eIsDownExceptio n( "Sorry, the db is currently not
working" , sqlex );
}
catch (Exception ex)
{
throw new MyCustomNonHand ledException ("Something unexpected happened."
, ex);
}
Its usually a good practice to take the "real" exception and make it the
".InnerExceptio n"...when you create your own exception classes. Which you
can handle with the overloaded constructor.

Most times I don't wrap 'em up for the user nice-like. But that's me.
Depending on the audience and application, you might want to wrap 'em up
nice-like.
............

"Bela Istok" <be****@hotmail .comwrote in message
news:%2******** **********@TK2M SFTNGP06.phx.gb l...
Nice post sloan, but this is the opposite site of the question. The
Cwalina
post talks about throwing exceptions not handling the exceptions. But it's
good to know that too.

Regards,

Bela Istok
>"sloan" <sl***@ipass.ne twrote in message
news:OR******* *********@TK2MS FTNGP04.phx.gbl ...
>>>
Check this entry out:
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx


"Sweetiecakes " <x@x.comwrote in message
news:48****** *************** **@news.fv.fi.. .
Hello

I'm a bit confused on how one should handle exceptions. I'm currently
building an ADO.NET Windows Forms application. I've built a class for
manipulati ng data within the database in question. This class is used
from forms. Let's assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to
show when giving an error message?

Is there any "centralize d" way of handling exceptions: for example,
having a centralized exception handler catch everything, and show an
error message relative to the caught exception?

Thanks


Aug 10 '08 #9
Jon,
And of course nothing *ever* goes wrong with valid SQL... The database
never goes down, there are never concurrency violations, locking
problems, failing to find expected data etc.
As far as I know them will the SQLException ignore all of those problems you
are showing

The SQLexception is needed to check if a stored procedure or an parameter in
that does not exist.
But you are in my opinion a jerk of a developer as you are testing that at
run time.

Cor
Aug 10 '08 #10

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

Similar topics

9
3205
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
28
2258
by: Frank Puck | last post by:
Meanwhile there are at least 8 years that compilers exist, which provide a working implementation of C++ Exception Handling. Has anything changed meanwhile? From my point of view nothing has changed -- people are still using a coding style, which I call pre-C++-Exception-Handling. Is this different in your experience? What is your experience regarding this subject? I think that a C++ IOstream lib, which is by standard free of exceptions...
9
2538
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
6
746
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 there a way to list exactly which exceptions that are not handlet? (design time) I could of cause ... use catch (Exception e) {...} as the final catch ... but I would prefer to now the "landscape" before doing that ..
34
2238
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 ////////////////////////////////// Try { for ( ... ){// Code that might throw exception} }catch(...){} //////////////////////////////////////////////////////////////////////////
2
5874
by: Rajeev Soni | last post by:
Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if there any exception is occurred in DAL, what is the best thing to do: 1. Dont catch the exception in DAL and let it prop up to the Application level and the Global.Application_Error event log it to any source and show let ASP.NET show custom error page provided in Web.Config file. OR 2....
16
2547
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following method: // --- Create an Exception Handler for Thread Exceptions ---------------- Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
5
3831
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 in the application log. The static method is overloaded, and supports passing exceptions and/or strings just like throwing an exception.The class will also fall back to the standard exception handling if something goes wrong in my class. As an...
35
3807
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 gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
35
3512
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly, especially as programs get large. Most of the issues of exceptions are not specific to Python, but I sometimes feel that Python makes them more acute because of the free-n- easy manner in which it employs exceptions for its own uses and allows users...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.