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

Exception derived class

I want to access the protected member hresult of the Exception class, so I
derived my own class from ApplicationException and SystemException.

public class LoggedException : SystemException
{
public LoggedException() : base()
{
}

public LoggedException(String message) : base (message)
{
}

public LoggedException(String message, Exception objInner) : base(message,
objInner)
{
}

public LoggedException(SerializationInfo srlInfo, StreamingContext
strmCntxt) : base(srlInfo, strmCntxt)
{
}
}

Now , I write the following code that raises SystemException, where obj is
still not allocated.Now my question is why it doesn't get caught in my catch
block, when my class is derived from SystemException.I have tried deriving
from Exception base class, but no use.How should I go about it, if I want to
access the protected member hresult of the exception class.
try
{
obj.GetType()
}
catch(LoggedException e)
{
//...........
}
Nov 16 '05 #1
3 3821
It is happening because you have derived LoggedException from
SystemException class. You can not deal with SystemException exception
in any of its devired class catch block. But its OK vice versa i-e
dealing with LoggedException inside SystemException's catch block.

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
Hi Jasleen,

You can not catch a System.Exception type exception under a LoggedException
catch block. In any case you can catch the System.Exception and raise your
own exception in the catch block.

<CODE>

try
{
obj.GetType()
}
catch(Exception)
{
raise new LoggedEsception();
}
catch(LoggedException e)
{
//...........
}

</CODE>

Hope it will help....

Cheers,
Rahul Anand

"Jasleen" wrote:
I want to access the protected member hresult of the Exception class, so I
derived my own class from ApplicationException and SystemException.

public class LoggedException : SystemException
{
public LoggedException() : base()
{
}

public LoggedException(String message) : base (message)
{
}

public LoggedException(String message, Exception objInner) : base(message,
objInner)
{
}

public LoggedException(SerializationInfo srlInfo, StreamingContext
strmCntxt) : base(srlInfo, strmCntxt)
{
}
}

Now , I write the following code that raises SystemException, where obj is
still not allocated.Now my question is why it doesn't get caught in my catch
block, when my class is derived from SystemException.I have tried deriving
from Exception base class, but no use.How should I go about it, if I want to
access the protected member hresult of the exception class.
try
{
obj.GetType()
}
catch(LoggedException e)
{
//...........
}

Nov 16 '05 #3
As Masqsood pointed out you cannot extend a base class and catch the derived
class's type instead of the base class's type. This is because the exception
that is thrown is the base type, not the extended type, and catch clauses
evaluate the exception and its base class, not its derived class.

However, what you can do is use reflection to access this normally
inaccessible field. A routine like this ought to do it...

private int GetHresult(Exception ex)
{
PropertyInfo pi =
ex.GetType().GetProperty("HResult",BindingFlags.In stance |
BindingFlags.NonPublic);
return (int)pi.GetValue(ex,null);
}

so in your catch handler you can do this...

try
{
obj.GetType(); // why this throws nobody knows....
}
catch(Exception e) // you really ought to catch a more specific type here.
{
int hr = GetHresult(ex);
// party on with the hresult value....
}
"Jasleen" <Ja*****@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
I want to access the protected member hresult of the Exception class, so I
derived my own class from ApplicationException and SystemException.

public class LoggedException : SystemException
{
public LoggedException() : base()
{
}

public LoggedException(String message) : base (message)
{
}

public LoggedException(String message, Exception objInner) :
base(message,
objInner)
{
}

public LoggedException(SerializationInfo srlInfo, StreamingContext
strmCntxt) : base(srlInfo, strmCntxt)
{
}
}

Now , I write the following code that raises SystemException, where obj is
still not allocated.Now my question is why it doesn't get caught in my
catch
block, when my class is derived from SystemException.I have tried deriving
from Exception base class, but no use.How should I go about it, if I want
to
access the protected member hresult of the exception class.
try
{
obj.GetType()
}
catch(LoggedException e)
{
//...........
}

Nov 16 '05 #4

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

Similar topics

7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
11
by: Dave | last post by:
try { ... } catch (exception e) { cout << e.what() << endl; } In the code above, e is caught by value rather than polymorphically (assume
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
5
by: hillcountry74 | last post by:
Hi, I'm a newbie to OOP and VB.Net. In the business layer, I have a base class, which is an abstract class in VB.Net. The derived class overrides a method and calls other methods in the base...
8
by: Kannan | last post by:
Some amount of memory is allocated inside the Base Constructor using new. During the construction of a derived object an exception occurred in the constructor of the derived class. Will the...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.