473,785 Members | 2,851 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
37 1910
Arne,

Did you see my msdn link about the SQLException and how will you get from
the server any information as you have not even a cable to it?

Cor

"Arne Vajhøj" <ar**@vajhoej.d kschreef in bericht
news:48******** *************** @news.sunsite.d k...
Cor Ligthert[MVP] wrote:
>"Arne Vajhøj" <ar**@vajhoej.d kschreef in bericht
news:48******* *************** *@news.sunsite. dk...
>>Cor Ligthert[MVP] wrote:
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.

No.

ADO.NET does not return status values.

Either it is a success all the way through or you get an
exception.

The only bad thing about the exception is that you get the
same exception type for too many different problems. I guess
that will be fixed in .NET 4.0 or 5.0 !
Can you point me where your answer is about. I don't have the idea to
have written anything about a Status.

There are only two alternatives for returning errors: exceptions and
status codes. So since .NET does not have status codes, then the
choices are somewhat limited-
>But what you wrote is in my opinion true, although you can catch with the
SQLException some information why the SQL transaction on the Server Side
went wrong.

You can, but that is very bad OOP.
> However, in my opinion a designer should fix all those
errors which are catched here fix before he gives his program in
production.

None of the problems Jon list is permanent problems. They can
happen in production even if they do not happen in test.

And even if the database server did go down under test, then I am not
quite sure what the designer should fix ? Not use a database ??
(Or catch them at client side as it is hardware dependend, I
have never seen a server, which was down, give any information)

I am not quite sure what you mean by client and server here, but
since it is about database access let us assume database client
and server.

If the database server si down, then the database client will
get an exception and need to handle it. Like Jon explained.

Arne
Aug 12 '08 #21

(a lot snipped)
>
How is a broken network connection a design time error? *Handling* the
broken network connection is a design-time issue. A program cannot
predict, prevent or flawlessly detect a broken network connection, no
matter how hard it tries. Sometimes things just go wrong as you're doing
them. Signaling that we unexpectedly cannot complete an operation is what
exceptions are *for*. Stress "unexpected ly".
How can an SQLException be given back by a Server that is not connected with
your client.?

http://msdn.microsoft.com/en-us/libr...exception.aspx
What on earth is "catching with the normal exception"? Do you mean a
catch-all exception handler for "things went wrong"?
http://msdn.microsoft.com/en-us/libr...exception.aspx

Cor

Aug 12 '08 #22
Cor Ligthert[MVP] wrote:
>
(a lot snipped)
>>
How is a broken network connection a design time error? *Handling* the
broken network connection is a design-time issue. A program cannot
predict, prevent or flawlessly detect a broken network connection, no
matter how hard it tries. Sometimes things just go wrong as you're
doing them. Signaling that we unexpectedly cannot complete an
operation is what exceptions are *for*. Stress "unexpected ly".
How can an SQLException be given back by a Server that is not connected
with your client.?
An SQLException is never "given back by a server", it's raised by a class
implementing database functionality. In some cases, the SQLException
represents an error condition indicated by the server. In other cases, it
represents an error condition detected independently from the server (being
unable to connect would be one).

Surely I'm not divulging some secret here, so I can't help but wonder what
you're getting at.

--
J.
Aug 12 '08 #23
Cor Ligthert[MVP] <no************ @planet.nlwrote :
(a lot snipped)

How is a broken network connection a design time error? *Handling* the
broken network connection is a design-time issue. A program cannot
predict, prevent or flawlessly detect a broken network connection, no
matter how hard it tries. Sometimes things just go wrong as you're doing
them. Signaling that we unexpectedly cannot complete an operation is what
exceptions are *for*. Stress "unexpected ly".
How can an SQLException be given back by a Server that is not connected with
your client.?

http://msdn.microsoft.com/en-us/libr...exception.aspx
Why not give it a try? That's what I've just done. Here's the exception
I received:

System.Data.Sql Client.SqlExcep tion: A network-related or instance-
specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow
remote connections. (provider: SQL Network Interfaces, error: 26 -
Error Locating Server/Instance Specified)

Satisfied now?

What exception do *you* think occurs (at the client side) when a SQL
database can't be reached?

--
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 12 '08 #24
From the top of the page from which I have sent you the link.

The exception that is thrown when SQL Server returns a warning or error. This class cannot be inherited.
So please tell Microsoft that they are wrong not me

Cor

"Jeroen Mostert" <jm******@xs4al l.nlschreef in bericht news:48******** *************@n ews.xs4all.nl.. .
Cor Ligthert[MVP] wrote:
>
(a lot snipped)
>>>
How is a broken network connection a design time error? *Handling* the
broken network connection is a design-time issue. A program cannot
predict, prevent or flawlessly detect a broken network connection, no
matter how hard it tries. Sometimes things just go wrong as you're
doing them. Signaling that we unexpectedly cannot complete an
operation is what exceptions are *for*. Stress "unexpected ly".
How can an SQLException be given back by a Server that is not connected
with your client.?
An SQLException is never "given back by a server", it's raised by a class
implementing database functionality. In some cases, the SQLException
represents an error condition indicated by the server. In other cases, it
represents an error condition detected independently from the server (being
unable to connect would be one).

Surely I'm not divulging some secret here, so I can't help but wonder what
you're getting at.

--
J.
Aug 12 '08 #25

Why did you try it, you was so sure of yourself.

But did you try the normal exception to see of this would not give almost
the same result in this not normal case.

My expirience is like that.

Cor

"Jon Skeet [C# MVP]" <sk***@pobox.co mschreef in bericht
news:MP******** *************@m snews.microsoft .com...
Cor Ligthert[MVP] <no************ @planet.nlwrote :
>(a lot snipped)
>
How is a broken network connection a design time error? *Handling* the
broken network connection is a design-time issue. A program cannot
predict, prevent or flawlessly detect a broken network connection, no
matter how hard it tries. Sometimes things just go wrong as you're
doing
them. Signaling that we unexpectedly cannot complete an operation is
what
exceptions are *for*. Stress "unexpected ly".
>How can an SQLException be given back by a Server that is not connected
with
your client.?

http://msdn.microsoft.com/en-us/libr...exception.aspx

Why not give it a try? That's what I've just done. Here's the exception
I received:

System.Data.Sql Client.SqlExcep tion: A network-related or instance-
specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow
remote connections. (provider: SQL Network Interfaces, error: 26 -
Error Locating Server/Instance Specified)

Satisfied now?

What exception do *you* think occurs (at the client side) when a SQL
database can't be reached?

--
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 12 '08 #26
Cor Ligthert[MVP] <no************ @planet.nlwrote :
Why did you try it, you was so sure of yourself.
So that I would be able to show the exact wording, as you seemed to
reject my assertion so completely.
But did you try the normal exception to see of this would not give almost
the same result in this not normal case.

My expirience is like that.
What do you mean by "try the normal exception"? Do you mean catching
just "Exception" ? Yes, of course that will catch it. But let's look at
the first post I responded to in this thread:

<quote author="Cor">
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.
</quote>

That's what I was disputing. There are various reasons why you may see
a SQL exception at execution time. SQL exceptions are to indicate
database errors, and those can occur for reasons other than "errors you
have made in your scripts" - and even errors in SQL may only be visible
in certain situations.

--
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 12 '08 #27
Cor Ligthert[MVP] wrote:
From the top of the page from which I have sent you the link.

The exception that is thrown when SQL Server returns a warning or error.
This class cannot be inherited.

So please tell Microsoft that they are wrong not me
The MSDN documentation is incomplete *and* you are wrong. I don't see how
these two exclude each other...

From the rest of the thread it's obvious that you're out to prove you're
right and the rest is wrong, even if you have to keep shifting the topic.
This discussion serves no further point, if indeed it ever served any.

--
J.
Aug 12 '08 #28
Cor Ligthert[MVP] wrote:
Did you see my msdn link about the SQLException and how will you get
from the server any information as you have not even a cable to it?
????

The exception object is created by the SQLServer ADO.NET provider
running client side, so no problem.

Arne
Aug 13 '08 #29
Cor Ligthert[MVP] wrote:
From the top of the page from which I have sent you the link.

The exception that is thrown when SQL Server returns a warning or error.
This class cannot be inherited.

So please tell Microsoft that they are wrong not me
You are completely wrong about this.

The documentation is not particular good. Whoever wrote
that must have assumed that it was obvious that no
connectivity would also result in that exception.
What else could happen ?

Arne
Aug 13 '08 #30

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

Similar topics

9
3206
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
2260
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
3832
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
3514
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...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10092
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
9950
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...
1
7500
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
6740
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
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.