Which kind of exception ?

G

giatkoet

Hello,

I would like to understand how it is possible when coding in C# to know
which type of exceptions can be thrown by some code placed on a
try/catch expression, so I can specify in the catch parameters which
type of exception I want to catch ?

I have to say this point has always been obscure to me ...

Thank you for your help.
 
A

Anders Eriksson

Hello,

I would like to understand how it is possible when coding in C# to know
which type of exceptions can be thrown by some code placed on a
try/catch expression, so I can specify in the catch parameters which
type of exception I want to catch ?
It depends on the functions you call inside the Try{}
For each function there is a specification in the documentation which
Exception this function uses.

E.g. Environment..::.GetFolderPath Method


ArgumentException
PlatformNotSupportedException

See http://msdn.microsoft.com/en-us/library/14tx8hby.aspx

// Anders
 
A

Arne Vajhøj

I would like to understand how it is possible when coding in C# to know
which type of exceptions can be thrown by some code placed on a
try/catch expression, so I can specify in the catch parameters which
type of exception I want to catch ?

I have to say this point has always been obscure to me ...

You cross your fingers and hope that the documentation
is accurate.

A bit of defensive programming just in case it is not can
be a very good thing.

Arne
 
A

Arne Vajhøj

Note that even in Java, it's possible for a method to throw an exception
it's not declared to be able to throw.

Yes. All unchecked exceptions.
One could create an exception
instance in a different method and return it, thus hiding the type from
the compiler,

No. That would either result in a compile time error or a runtime
ClassCastException.
or could call a method via reflection.

No. That would result in another checked exception
InvocationTargetException with the given exception nested.

Arne
 
A

Arne Vajhøj

Yes. All unchecked exceptions.

I'm not just talking about unchecked exceptions. I'm talking about
exceptions that may be thrown without being able to tell from the code
exactly _which_ exception might be thrown.
No. That would either result in a compile time error or a runtime
ClassCastException.

I don't think you understood what I wrote. For example:

public static void main(String[] args)
{
try
{
throwException();
}
catch (Exception e)
{ }
}

static void throwException() throws Exception
{
throw getException();
}

static Exception getException()
{
return new java.io.IOException("dummy exception");
}

That compiles fine without any errors. But you can't tell from the
declaration of throwException() exactly which exception is thrown.

You don't need to have something return an instance
of Exception to do that.

static void throwException() throws Exception {
throw new IOException("dummy exception");
}

does the same thing.

That is how a language that supports polymorphism works.

static Object giveMeSomething() {
return "ABC"
}

is the same thing.

But that is (or should be) a design choice - by returning
something more general it allows for later changes to
implementation without changing the API.

.... throws Exception {

means that API tells the caller to expect any sub class
of Exception.

That with Exception is rarely a good design, but it could be a good
design for some more specific base classes that still
have sub classes.

Arne
 
R

Robert Bryan

Hello,

I would like to understand how it is possible when coding in C# to know
which type of exceptions can be thrown by some code placed on a
try/catch expression, so I can specify in the catch parameters which
type of exception I want to catch ?

I have to say this point has always been obscure to me ...

Thank you for your help.

Looks to me like you could use some code examples of how to throw and
catch different exceptions:

try
{
// Do some stuff.
...
if (AppErr)
{
// Throw an appliation exception with a message that can be
retreived from the Exception.Message method.
throw (new ApplicationException("This is an Application
Exception msg that occurred because of blah, blah"));
}
else if (GeneralErr)
{
// Throw a more general exception
throw (new SystemException("This is a System Exception msg
that occurred because of blah, blah, blah"));
}
}

catch (ApplicationException)
{
// Get the type of the exeception from the Exception object.
String S = "Application Exception type = " +
ex.GetType().ToString();
DispUIMsg(S);
// Get the message associated with the exception.
S = "The reason for the error is because of = " ex.Message;
DispUIMsg(S);
}

catch (System.Exception ex)
{
// Get the type of the exeception from the Exception object.
String S = "System Exception type = " + ex.GetType().ToString();
DispUIMsg(S);
// Get the message associated with the exception.
S = "The reason for the error is because of = " ex.Message;
DispUIMsg(S);
}

catch
{
// This is the last ditch error handler. Most of the time
exceptions should be caught by one of the exception
// handlers above. If they don't catch it, then this one should.
But, there is no message information.
String S = "Problem - an unknown exception has been thrown.";
DispUIMsg(S);
}

When debugging, you can tell the debugger to catch an exception when
it is thrown. This is handy to help track down bugs from exceptions
right after they are thrown rather than having it wonder up the call
stack looking for an exception handler. Exception settings are
available from the Debug / Exceptions menu item from Visual Studio

Bob Bryan MCSD
Please visit my blog:
http://designingefficientsoftware.wordpress.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top