473,385 Members | 1,642 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.

How to extract all exceptions for any method

Hi

I would like to extract all possible exceptions for a particular
method. Ideally this would be a Visual Studio 2005 Add-In and would
allow a developer to highlight a any method, pressing a shortcut key
which would result in a try/catch with all exception(s) handled.

Would someone nice out there help me on this one as this process gets
ignored by my team because it takes took long.

Regards,
Paul
Jun 27 '08 #1
9 2505
Unlike java, C# doesn't enforce specific exception handling. You can
add notes that an exception might be expected, but you can get many
more at any time (OutOfMemoryException, SecurityException [principal],
ThreadAbortException, etc).

To indicate specific expected exceptions, use /// comments:

/// <exception cref="FooException">Throws a Foo when you Bar</
exception>

Still! In most cases, exception handling is limited to rolling back
any damage, perhaps logging, and re-throwing. In my experience, it is
very rare that there are *lots* of expected exceptions with different
handling strategies - generally I might expect one or two special
cases, and let the rest just raise through the stack (with higher-up
code worrying about it)... and you can handle the one-or-two easily
enough from comments.

Additionally, constructs like "using" and "lock" can be used to ensure
that resources are released cleanly upon exit (even through
exception).

Did you have a specific scenario in mind?

Marc
Jun 27 '08 #2
On Apr 15, 6:12*am, paul <p...@bobbob.netwrote:
I would like to extract all possible exceptions for a particular
method. *Ideally this would be a Visual Studio 2005 Add-In and would
allow a developer to highlight a any method, pressing a shortcut key
which would result in a try/catch with all exception(s) handled.
There's no way of doing this, because there's no way of reliably
predicting what exceptions a method could throw. For instance, there
are a bunch of exceptions which can occur at any time (threads being
aborted, app domains being unloaded, out of memory) and even if you
recursively try to work out which exceptions a method throws directly,
and which exceptions are thrown by methods that original method calls
(etc) it would turn into an unworkable nightmare very quickly.
Would someone nice out there help me on this one as this process gets
ignored by my team because it takes took long.
It sounds like you're regularly trying to catch everything, which is
usually a bad idea. Generally, you should have far more try/finally
blocks (usually in the form of "using" statements) than try/catch
blocks.

Only catch exceptions you're actually able to handle, unless you're
pretty much at the top of a thread, where you *might* (depending on
context) want to catch everything and react appropriately (e.g.
logging before quitting, or aborting that request for a server type
application).

Jon
Jun 27 '08 #3
On Apr 15, 11:39*pm, Marc Gravell <marc.grav...@gmail.comwrote:
Unlike java, C# doesn't enforce specific exception handling. You can
add notes that an exception might be expected, but you can get many
more at any time (OutOfMemoryException, SecurityException [principal],
ThreadAbortException, etc).

To indicate specific expected exceptions, use /// comments:

/// <exception cref="FooException">Throws a Foo when you Bar</
exception>

Still! In most cases, exception handling is limited to rolling back
any damage, perhaps logging, and re-throwing. In my experience, it is
very rare that there are *lots* of expected exceptions with different
handling strategies - generally I might expect one or two special
cases, and let the rest just raise through the stack (with higher-up
code worrying about it)... and you can handle the one-or-two easily
enough from comments.

Additionally, constructs like "using" and "lock" can be used to ensure
that resources are released cleanly upon exit (even through
exception).

Did you have a specific scenario in mind?

Marc
Hi

Thank you for the info.

I am pretty sure this can be done as Visual Studio 2005 already tells
you the specific Exceptions that can be generated by a single method.
Also Red-Gate Exception hunter validates your code and highlights all
possible exceptions (part from generic exception).

For example:

Old code:

File.Open(..);

Would in a result like this

try
{
File.Open(..);
}
Catch (FileNotFoundException ex)
{
..
}
Catch (DirectoryNotFound ex)
{
...
}
Catch (Exception ex)
{
...
}

Regards,
Jun 27 '08 #4
On Tue, 15 Apr 2008 14:01:47 -0700, paul <pa**@bobbob.netwrote:
I am pretty sure this can be done as Visual Studio 2005 already tells
you the specific Exceptions that can be generated by a single method.
I'm not aware of any feature in VS that does this generally. It's true
that the documentation for much of .NET includes information as to which
exceptions may be thrown, and inasmuch as VS shows you that documentation,
it will show you those specific exceptions.

If you know of something more specific than that, perhaps you could
provide more details regarding what you're seeing in VS.
Also Red-Gate Exception hunter validates your code and highlights all
possible exceptions (part from generic exception).
I'm not familiar with that tool. However, it's plainly false that it
"highlights all possible exceptions". No doubt it does a good job
traversing the call chain (something that's easily done via reflection),
looking for code that creates and throws exceptions. But it's simply not
possible to find literally all possible exceptions.

As Marc already asked, was there some specific situation or scenario
you're trying to address? Generally speaking, if it's possible for a
method to throw some exception that can be usefully dealt with, it will be
documented by the author of that method. Other kinds of exceptions that
might be thrown wouldn't be relevant information anyway, by definition.

Pete
Jun 27 '08 #5
On Apr 16, 7:12*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 15 Apr 2008 14:01:47 -0700, paul <p...@bobbob.netwrote:
I am pretty sure this can be done as Visual Studio 2005 already tells
you the specific Exceptions that can be generated by a single method.

I'm not aware of any feature in VS that does this generally. *It's true *
that the documentation for much of .NET includes information as to which *
exceptions may be thrown, and inasmuch as VS shows you that documentation,*
it will show you those specific exceptions.

If you know of something more specific than that, perhaps you could *
provide more details regarding what you're seeing in VS.
Also Red-Gate Exception hunter validates your code and highlights all
possible exceptions (part from generic exception).

I'm not familiar with that tool. *However, it's plainly false that it *
"highlights all possible exceptions". *No doubt it does a good job *
traversing the call chain (something that's easily done via reflection), *
looking for code that creates and throws exceptions. *But it's simply not *
possible to find literally all possible exceptions.

As Marc already asked, was there some specific situation or scenario *
you're trying to address? *Generally speaking, if it's possible for a *
method to throw some exception that can be usefully dealt with, it will be*
documented by the author of that method. *Other kinds of exceptions that*
might be thrown wouldn't be relevant information anyway, by definition.

Pete
Hi

This is possible and it's standard in VS Studio 2005 (C#).

Hover your mouse over a method (e.g. System.IO.File.Open) and wait for
the little popup window, here lists all known exceptions (again part
from generic exceptions), so it is possible.

Paul
Jun 27 '08 #6
On Tue, 15 Apr 2008 17:32:48 -0700, paul <pa**@bobbob.netwrote:
This is possible and it's standard in VS Studio 2005 (C#).

Hover your mouse over a method (e.g. System.IO.File.Open) and wait for
the little popup window, here lists all known exceptions (again part
from generic exceptions), so it is possible.
Yup. That's part of the documentation. It exists only because the author
of the class (in this case, Microsoft) included that information. It
doesn't do it for any random method, and it doesn't show you literally
every exception that could be thrown.

Pete
Jun 27 '08 #7
On Apr 16, 11:08*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 15 Apr 2008 17:32:48 -0700, paul <p...@bobbob.netwrote:
This is possible and it's standard in VS Studio 2005 (C#).
Hover your mouse over a method (e.g. System.IO.File.Open) and wait for
the little popup window, here lists all known exceptions (again part
from generic exceptions), so it is possible.

Yup. *That's part of the documentation. *It exists only because the author *
of the class (in this case, Microsoft) included that information. *It *
doesn't do it for any random method, and it doesn't show you literally *
every exception that could be thrown.

Pete
OK, so how can I get this information (and I am not expecting that
every single method will have this).

Regards,
Jun 27 '08 #8
On Tue, 15 Apr 2008 19:34:53 -0700, paul <pa**@bobbob.netwrote:
OK, so how can I get this information (and I am not expecting that
every single method will have this).
What do you mean by "get"? I mean, you can hover your mouse over the
method name in VS. That will get you the information that's in the
documentation. You could use the "Red-Gate" tool you mentioned earlier to
do a code analysis, extracting what information it can from the code
itself.

At what point are you trying to "get" this information? How do you expect
to use it?

Pete
Jun 27 '08 #9
On Apr 16, 4:34 am, paul <p...@bobbob.netwrote:
On Apr 16, 11:08 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 15 Apr 2008 17:32:48 -0700, paul <p...@bobbob.netwrote:
This is possible and it's standard in VS Studio 2005 (C#).
Hover your mouse over a method (e.g. System.IO.File.Open) and wait for
the little popup window, here lists all known exceptions (again part
from generic exceptions), so it is possible.
Yup. That's part of the documentation. It exists only because the author
of the class (in this case, Microsoft) included that information. It
doesn't do it for any random method, and it doesn't show you literally
every exception that could be thrown.
Pete

OK, so how can I get this information (and I am not expecting that
every single method will have this).

Regards,
you could "get" the info from the xml file where the documentation for
a specific assembly can be found.
-- Henon
Jun 27 '08 #10

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

Similar topics

26
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal...
4
by: Johannes Hansen | last post by:
What are the best practice on handling an exception caused by a Dispose method when its called from inside a loop? Wrap the entire loop in a try-catch or do the try-catch on each iteration to get...
6
by: Iain | last post by:
Hey folks, (I posted this in microsoft.public.dotnet.csharp.general yesterday, but it appears that this group is rather more lively) For the application I am developing, I have a data access...
2
by: felecha | last post by:
I learned about Exceptions in School, and now that I'm building my first real application on the job, I'm going through it trying to analyze it for all possible Exceptions so I can handle them...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
7
by: matt | last post by:
hello, is there a way to programmatically loadup one of your .aspx pages, as if it had been rendered to an actual request? something like: //create the page in memory SomePage somePage =...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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: 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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.