Can't catch exception

P

Pete Davis

I've got the following code:

try
{
reader = new StreamReader(configFile);
XmlSerializer serializer = new XmlSerializer(typeof(ServerConfig));
config = (ServerConfig) serializer.Deserialize(reader);
}
catch(System.InvalidOperationException ioe)
{
Debug.WriteLine(ioe.ToString());
}
finally
{
reader.Close();
}

In this case, it throws an InvalidOperationException in the constructor for
the XmlSerializer(). I know why it's throwing the exception, and I can fix
it, but in the future, I'd like to catch it and handle it. Why can't I catch
it here?

The specific message is:

Unhandled Exception: System.InvalidOperationException: There is an error in
XML document (22, 4).

and it goes on a bit... Any ideas?

Pete
 
R

Richard

1) Are you sure it's not throwing the exception from the
finally block? - If it is throwing from the Close method
then you'll have problems...

2) What happens if you catch any System.Exception instead
of a InvalidOperationException?

--Richard
 
W

wandering1

Pete Davis said:
I've got the following code:

try
{
reader = new StreamReader(configFile);
XmlSerializer serializer = new XmlSerializer(typeof(ServerConfig));
config = (ServerConfig) serializer.Deserialize(reader);
}
catch(System.InvalidOperationException ioe)
{
Debug.WriteLine(ioe.ToString());
}
finally
{
reader.Close();
}

In this case, it throws an InvalidOperationException in the constructor for
the XmlSerializer(). I know why it's throwing the exception, and I can fix
it, but in the future, I'd like to catch it and handle it. Why can't I catch
it here?

The specific message is:

Unhandled Exception: System.InvalidOperationException: There is an error in
XML document (22, 4).

and it goes on a bit... Any ideas?

Pete

Your code only seems to be looking for one exception type.
Assuming configfile is a string and you are using:
public StreamReader(string path);

You should also be looking to catch these exceptions:

ArgumentException: path is an empty string ("").
ArgumentNullException: path is a null reference (Nothing in Visual
Basic).
FileNotFoundException: The file cannot be found.
DirectoryNotFoundException: The specified path is invalid, such as
being on an unmapped drive.
IOException: path includes an incorrect or invalid syntax for file
name, directory name, or volume label.

and if you need a catchall use

try{
}catch(System.InvalidOperationException ioe){
}catch(Exception ex){
}finally{
}
 
S

Simon Smith

On Mon, 16 Feb 2004 17:26:00 GMT in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , "Pete Davis"
I've got the following code:

try
{
reader = new StreamReader(configFile);
XmlSerializer serializer = new XmlSerializer(typeof(ServerConfig));
config = (ServerConfig) serializer.Deserialize(reader);
}
catch(System.InvalidOperationException ioe)
{
Debug.WriteLine(ioe.ToString());
}
finally
{
reader.Close();
}

In this case, it throws an InvalidOperationException in the constructor for
the XmlSerializer(). I know why it's throwing the exception, and I can fix
it, but in the future, I'd like to catch it and handle it. Why can't I catch
it here?

The specific message is:

Unhandled Exception: System.InvalidOperationException: There is an error in
XML document (22, 4).

and it goes on a bit... Any ideas?
As someone else said, it looks like it's happening in the finally
clause.
You create reader and presumably set it to null before the Try. In the
try you set it to something. If it errors on that then when it gets to
the finally it'll still be null and .Close() won't do anything
useful....
finally should look something like :

} finally {
if (reader != null) reader.Close();
}
 

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