473,405 Members | 2,272 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,405 software developers and data experts.

Catching exceptions but don't want to cast the type.

UJ
I have a try catch where I don't know all of the exceptions that can be
thrown (I'm calling a web service in the try) and what I would ideally like
to do is a catch all but then look at the type of exception that was thrown
and decide what to do there. Because I don't know what all can be thrown I
need to have sort of a generic exception where I can then say - what type is
it ? and then process accordingly. I don't want to do a (Execption ex)
because that will lose some information - or at least I think it will.

I've tried doing catch (object ex) and it won't let me. Object must be
derived from System.Exception.

TIA - Jeff.
Mar 24 '06 #1
5 1381
UJ
Nevermind folks. I found out that even if you say (Exception ex) you can
still find out what type it is and go from there.

Thanks anyway.

"UJ" <fr**@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have a try catch where I don't know all of the exceptions that can be
thrown (I'm calling a web service in the try) and what I would ideally like
to do is a catch all but then look at the type of exception that was thrown
and decide what to do there. Because I don't know what all can be thrown I
need to have sort of a generic exception where I can then say - what type
is it ? and then process accordingly. I don't want to do a (Execption ex)
because that will lose some information - or at least I think it will.

I've tried doing catch (object ex) and it won't let me. Object must be
derived from System.Exception.

TIA - Jeff.

Mar 24 '06 #2
Don't 'think' - try it. You might be surprised at what you find.
"UJ" <fr**@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have a try catch where I don't know all of the exceptions that can be
thrown (I'm calling a web service in the try) and what I would ideally like
to do is a catch all but then look at the type of exception that was thrown
and decide what to do there. Because I don't know what all can be thrown I
need to have sort of a generic exception where I can then say - what type
is it ? and then process accordingly. I don't want to do a (Execption ex)
because that will lose some information - or at least I think it will.

I've tried doing catch (object ex) and it won't let me. Object must be
derived from System.Exception.

TIA - Jeff.

Mar 24 '06 #3
Hi Jeff,

You can also catch various exceptions and end with the general exception.

try
{
// risky stuff
}
catch(SpecificException 1)
{
}
catch(SpecificException 2)
{
}
catch(SemiGeneralException)
{
}
catch(Exception e)
{
}

The order needs to be sorted by inheritance as everything that inherits a
certain exception will be caught by that catch block.
--
Happy Coding!
Morten Wennevik [C# MVP]
Mar 24 '06 #4
UJ
Thanks everybody for the help. I guess my real question was what do you do
when you don't know what exceptions can be thrown. I assumed that since you
said (Exception ex) that would mean that ex would be type exception. But it
turns out that is not true. ex will be of whatever type it was. So now I can
at least put in the checks that I know will happen and then do the all else
and keep track of what it found so I can add it later.

Thanks again.

"Stephany Young" <noone@localhost> wrote in message
news:uM**************@tk2msftngp13.phx.gbl...
Don't 'think' - try it. You might be surprised at what you find.
"UJ" <fr**@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have a try catch where I don't know all of the exceptions that can be
thrown (I'm calling a web service in the try) and what I would ideally
like to do is a catch all but then look at the type of exception that was
thrown and decide what to do there. Because I don't know what all can be
thrown I need to have sort of a generic exception where I can then say -
what type is it ? and then process accordingly. I don't want to do a
(Execption ex) because that will lose some information - or at least I
think it will.

I've tried doing catch (object ex) and it won't let me. Object must be
derived from System.Exception.

TIA - Jeff.


Mar 24 '06 #5
On Fri, 24 Mar 2006 08:53:59 -0500, UJ wrote:
Thanks everybody for the help. I guess my real question was what do you do
when you don't know what exceptions can be thrown. I assumed that since you
said (Exception ex) that would mean that ex would be type exception. But it
turns out that is not true. ex will be of whatever type it was. So now I can
at least put in the checks that I know will happen and then do the all else
and keep track of what it found so I can add it later.


Note that in your particular case, the only exceptions that can AFAIK be
thrown by a web service call are WebException and SoapException. So you it
seems a bit overkill to catch everything and then filter. You could do
something like:

try
{
// Call Web Service Method
}
catch (SoapException ex)
{
// Error occured when processing the web service request on the Web Server
}
catch (WebException (ex)
{
// Network error (host not found, 404...)
}
Mar 24 '06 #6

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

Similar topics

8
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
4
by: Laxmikant Rashinkar | last post by:
Hi, I am working with a program that someone else wrote. This program is full of bugs and unhandled exceptions are thrown all over the code. Instead of trying to catch the exception at each...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
3
by: Peter A. Schott | last post by:
I know there's got to be an easy way to do this - I want a way to catch the error text that would normally be shown in an interactive session and put that value into a string I can use later. I've...
5
by: Paul Sijben | last post by:
All, in a worker thread setup that communicates via queues is it possible to catch exceptions raised by the worker executed, put them in an object and send them over the queue to another thread...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
12
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
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
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,...
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...
0
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...
0
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,...

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.