473,837 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

try catch just does not work

I'm writing an app on the PDA using C# with .net 1.1. It is all working well
except in some cases a try catch is simply ignored and a totally different
error is returned. I've got code like below to execute a command. I'm
simulating loss of connection by stopping sqlserver during a transaction.
The problem is I cannot trap the error and I get a stack overflow exception
way up the stack at a Form.ShowDialog (). I've done a break on
executenonquery below and then pushed F11 and the catches both get ignored
and execution jumps up the stack without any code running at all.

I know this is a bit of an obsure problem but I appreciate any input. I
couldn't find anything on google.
Regards,
Michael

try
{
_Command.Execut eNonQuery();
}
catch(SqlExcept ion ex)
{
throw new Exception(ex.Me ssage);
}
catch(Exception ex)
{
throw new Exception(ex.Me ssage);
}
Jun 8 '06 #1
3 3806
This may not answer your problem directly, but what is the purpose of your
try/catch statement? What is happening (if it did work) is that your
catching an exception, creating a new instace of the exception and throwing
it. This will erase your stack trace which will track down the root of where
the orginal exception gets thrown. If all you need to do is bubble it up to
the calling method, take out the try/catch all together, that way you
preserve the stack trace in your exception.

Or if you want to personalize your error messages you can do something like
this:

try
{
_Command.Execut eNonQuery();
}
catch(SqlExcept ion ex)
{
string errMsg = "SQL Error received: " + ex.Message
throw new SqlException(er rMsg, ex); //passing an inner exception
parameter preserves your stack trace in the exception.
}
catch(Exception ex)
{
string errMsg = "Unexpected error occured while executing non query:
" +
ex.Message
throw new Exception(errMs g, ex);
}

Personally, I would leave out the try/catch statement.

"Michael C" wrote:
I'm writing an app on the PDA using C# with .net 1.1. It is all working well
except in some cases a try catch is simply ignored and a totally different
error is returned. I've got code like below to execute a command. I'm
simulating loss of connection by stopping sqlserver during a transaction.
The problem is I cannot trap the error and I get a stack overflow exception
way up the stack at a Form.ShowDialog (). I've done a break on
executenonquery below and then pushed F11 and the catches both get ignored
and execution jumps up the stack without any code running at all.

I know this is a bit of an obsure problem but I appreciate any input. I
couldn't find anything on google.
Regards,
Michael

try
{
_Command.Execut eNonQuery();
}
catch(SqlExcept ion ex)
{
throw new Exception(ex.Me ssage);
}
catch(Exception ex)
{
throw new Exception(ex.Me ssage);
}

Jun 8 '06 #2

If you research "Brad Abrams" and try catch finally, you'll find a blog.

Basically, a good rule of thumb is that you should have alot more
try/finally
statements

instead of
try/catch
or
try/catch/finally
statements.

try
{
//do something
}
finally
{
//close up resources like IDataReader for instance
}
Then somewhere, maybe the presentation layer....... you have the catch
statement.

Its actually wasted effort to catch and an an exception, and then rethrow
it.

...

The previous poster is also correct. You don't throw a new Exception.
Well, perhaps you want a specific user friendly message to display. Then
you ~should use the inner exception mechanism the other poster talks about.

If you did this:

try
{
}
catch(Exception ex)
{
throw ex;
}

that would be closer to what you want I think.... However, you need to look
at the first part of my post, which talks about only a try/finally

............... ............... ......


"Michael C" <no****@nospam. com> wrote in message
news:Om******** ******@TK2MSFTN GP02.phx.gbl...
I'm writing an app on the PDA using C# with .net 1.1. It is all working well except in some cases a try catch is simply ignored and a totally different
error is returned. I've got code like below to execute a command. I'm
simulating loss of connection by stopping sqlserver during a transaction.
The problem is I cannot trap the error and I get a stack overflow exception way up the stack at a Form.ShowDialog (). I've done a break on
executenonquery below and then pushed F11 and the catches both get ignored
and execution jumps up the stack without any code running at all.

I know this is a bit of an obsure problem but I appreciate any input. I
couldn't find anything on google.
Regards,
Michael

try
{
_Command.Execut eNonQuery();
}
catch(SqlExcept ion ex)
{
throw new Exception(ex.Me ssage);
}
catch(Exception ex)
{
throw new Exception(ex.Me ssage);
}

Jun 8 '06 #3
"sloan" <sl***@ipass.ne t> wrote in message
news:uh******** ******@TK2MSFTN GP03.phx.gbl...
Its actually wasted effort to catch and an an exception, and then rethrow
it.


That's true but this is only sample code. This statement had no try catch at
all and the catch was higher up in the call stack. I added these catches
just as a test giving me somewhere to add a breakpoint while keeping the
catch statements higher up working. I thought having the catch directly
around the statement and throwing it again might eliminate the problem but
unfortunately it didn't.

Michael
Jun 13 '06 #4

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

Similar topics

7
2256
by: Adrian Stutz [anime_layer] | last post by:
Hello, I try to do a mysql backup using mysqldump. I've written a php script that uses system() to execute mysqldump. I have two unix servers and the script runs fine on the frist one but fails without any error on the second server. I have following script to test it: ---------------------------------- <?php print system ('/usr/bin/mysqldump --opt -u user -ppassword database 2> /home/user/public_html/backup/error.txt | gzip >
2
1737
by: Eclectic | last post by:
Hey all, First up, I am a newb, so go easy on me! I am having issues with the FTP Function ... I have read MANY posts on similar problems, but have found no fixes that seem to work (for me at least) ... I have pretty much cut and paste the FTP example from PHP.net. The code connects to and logs into the FTP server fine, but never uploads
3
4084
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It works fine. But When a user accesses through Internet, this does not work.
4
4509
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
7
3632
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title> </head> <style type="text/css">
6
2447
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: /////////////////////////////////////////////// <% ntlogon = (Request.ServerVariables("LOGON_USER"))
4
2894
by: Das | last post by:
Hi, I have made an application in ASP.net with C#. The application works fine with localhost. I have uploaded the site. I'm using web user controls in the form. but some of the button do not work when they are clicked. One thing that I have found that they are nested user controls. which do not work. I'm unable to understand what is the problem with that. thanks in advance. Das
1
3138
by: Newbie in ChiTown | last post by:
Here's my code: I am using MS Access and I am trying to update a table (InvoiceDetails) with data input by the user on a form. However, it does not update nor does it give me an error message. Code listed below. Also, can you recommend a book on SQL. Thanks. Dim con As Object Dim rs As ADODB.Recordset
11
3400
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled tasks. How do I get it to work? Thanks, Jim
0
9842
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
9682
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,...
0
10874
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10271
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
9398
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
7806
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...
1
4474
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
2
4040
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3124
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.