473,383 Members | 1,815 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,383 software developers and data experts.

Bug in exception handling?

ben
I have a custom exception

class MyException: System.Runtime.InteropServices.COMException
{
.....
}

and code like this
try
{
.....
}
catch(MyException ex)
{
//Some code
}
catch(Exception ex)
{
// Other code
}

I put a break point in the MyException catch block, and another one in
the Exception catch block. I am running into a situation where the
debugger stops in the Exception catch block with out ever stopping in
the MyException catch. I check the type of the exception in the Locals
window only to find that it's of type MyException. I'm pretty sure that
it's supposed to be caught in the first catch not the second. What
could be going wrong?

May 26 '06 #1
3 1213
<be*@peikes.com> wrote:
I have a custom exception
<snip>
I put a break point in the MyException catch block, and another one in
the Exception catch block. I am running into a situation where the
debugger stops in the Exception catch block with out ever stopping in
the MyException catch. I check the type of the exception in the Locals
window only to find that it's of type MyException. I'm pretty sure that
it's supposed to be caught in the first catch not the second. What
could be going wrong?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

It may just be a debugger issue, of course - that's more likely than it
failing when running in release mode.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '06 #2
You might want to try catching COMException. Because your custom exception
extends COMException, the COMException handler should catch your exception.

Also, just FYI, it is generally recommended that custom exceptions extend
ApplicationException as documented in the ApplicationException documentation
and elsewhere on MSDN.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"be*@peikes.com" wrote:
I have a custom exception

class MyException: System.Runtime.InteropServices.COMException
{
.....
}

and code like this
try
{
.....
}
catch(MyException ex)
{
//Some code
}
catch(Exception ex)
{
// Other code
}

I put a break point in the MyException catch block, and another one in
the Exception catch block. I am running into a situation where the
debugger stops in the Exception catch block with out ever stopping in
the MyException catch. I check the type of the exception in the Locals
window only to find that it's of type MyException. I'm pretty sure that
it's supposed to be caught in the first catch not the second. What
could be going wrong?

May 28 '06 #3
Dale <da******@nospam.nospam> wrote:
Also, just FYI, it is generally recommended that custom exceptions extend
ApplicationException as documented in the ApplicationException documentation
and elsewhere on MSDN.


This recommendation has been rescinded! Check section 7.3.2 of the
Framework Design Guidelines book, for example, or here:

http://blogs.msdn.com/brada/archive/...27/402801.aspx

The rationale in the book is from Jeffrey Richter. To paraphrase, the
idea behind ApplicationException was to separate CLR exceptions from
your own exceptions. However, CLR exceptions such as
TargetInvocationException is derived from ApplicationException, so the
class has lost its meaning and raison d'etre.

-- Barry

--
http://barrkel.blogspot.com/
May 28 '06 #4

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

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.