On Tue, 16 Sep 2008 09:25:02 -0700, Raj <Ra*@discussions.microsoft.com>
wrote:
How do I know which methods will throw exception when I am using FCL or
other
third party .Net library?
You need to ask the author's of such third-party libraries.
Ideally, as is the case for the .NET documentation, they will clearly
document what exceptions may be thrown. But that's up to each individual
author of a library. There's no general rule that will work for all of
them.
I am developer of mostly native Windows applications and now .Net. After
working few months in Java, I am thinking why Win32 APIs or even .Net
documentation not clear on which methods will throw exception or what
exceptions can be expected.
I don't understand the statement. Win32 APIs do not, as a general rule,
throw exceptions. They return errors. .NET APIs do, as a general rule,
throw exceptions on failure, and that is always documented.
Jave APIs clearly define exceptions that can be
expected and enforces that we handle them.
Almost true. The fact is, in Java there are exceptions that are enforced
and those that aren't. Only the ones that are enforced are "clearly
defined" and "enforced that we handle them". So, even in Java you have
the possibility of an exception that's not documented but which could
still be thrown.
That said, if your real question is truly (as your Subject: line suggests)
"when to handle exceptions?", the answer is that you should handle them
when you can do something useful with it. If it makes sense for the
method in which you are considering handling the exception to actually
catch and process the exception, then do so. In the vast majority of
cases, this implies (just as in Java) that the caller of the method will
not ever know about the exception, not even the possibility of the
exception.
In other words, "do something useful with it" is another way of saying
that the method catching the exception can consider itself to have
successfully accomplished its purpose, even if an exception was thrown and
caught during the execution of that method.
If your method can't do something useful with the exception, then don't
catch it.
Can someone explain Windows philosophy on exception handling? I read lot
of
books/help on how to do exception handling but never clearly saw when to
do?
The article that sloan mentions seems to be a reasonable discussion to
me. But, keep in mind that it's more about .NET than about Windows
generally. Windows has evolved over more than two decades and as such is
not going to have a uniform philosophy. It's also more about designing
code that might _throw_ exceptions, rather than being about designing code
that might need to _handle_ exceptions. So it may have limited utility in
the context of your question here.
Pete