using System;
using System.IO;
using System.Xml;
namespace MyNamespace
{
class MainApp
{
[STAThread]
public static void Main(String[] args)
{
// Using instantiation to allow Main to access a non-
static function
MainApp app = new MainApp();
app.GetApplicationSettings();
}
private void GetApplicationSettings()
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load("filedoesntexist")
}
catch(System.IO.FileNotFoundException e)
{
// show stacktrace
}
}
}
}
When I try to load the file (which doesn't exist) the line
"xmlDoc.Load("filedoesntexist")" throws a FileNotFoundException. The
problem is, the exception falls through the function and back to Main,
where it isn't handled. Why does this happen? I'm still trying to
understand the inner workings of the .NET framework. Any help and/or
explanation is very much appreciated. Thanks
Are you sure you did see the FileNotFound exception?
"tcomer" <tco...@gmail.com> ha scritto nel messaggio
news:1171032602.1...@q2g2000cwa.googlegroups.com...
John
Anyways I cannot repro the probelm. In my test the try/catch block does
catches the exception correctly,
tcomer, are you sure that this is the exception that falls through to the
Main method?
--
HTH
Stoitcho Goutsev (100)
"Laura T." <L...@NOWHERE.COM> wrote in message
news:eyOJbxFT...@TK2MSFTNGP03.phx.gbl...
and
"Are you sure you did see the FileNotFound exception?"
This is the exception text:
"A first chance exception of type 'System.IO.FileNotFoundException'
occurred in System.Xml.dll
Additional information: Could not find file 'C:\App\Settings
\config.xml'."
The reason that I believe the exception is falling back through to
Main is because I can handle the exception if I place the call the
GetApplicationSettings() in a try/catch block and catch it that way.
But with the code in the original post, I get the exception every time
I run the application.
Even though it's possible to catch the exception in the Main method, I
really don't want Main to be aware of the exception, as Main is not
the method in which the exception occurs. Sorry if that sounds
confusing.
Hi,
I ran your code and it behaved correctly. Take the code exactly as it
is from your post and try running it. Post back and let us know what
happened.
Brian
I commented out everything I had and copy/paste the code from the
original post and get the same output, which is this:
"A first chance exception of type 'System.IO.FileNotFoundException'
occurred in System.Xml.dll
Additional information: Could not find file 'C:\App\Settings
\config.xml'."
I'm not exactly sure why this is happening. It's odd that it works for
some of you, yet I'm still having problems getting it to run properly.
Hi,
There's something unusual going on. I would expect the exception
message to refer to the file "filedoesnotexist" instead of "C:\App
\Settings\config.xml". Try this, create a brand new project that is
completely separate from the existing project and paste the code from
the post into it and reply back. I want to make sure you're starting
from clean slate.
Brian
--
HTH
Stoitcho Goutsev (100)
"tcomer" <tco...@gmail.com> wrote in message
news:1171039538.8...@h3g2000cwc.googlegroups.com...
--
HTH
Stoitcho Goutsev (100)
"tcomer" <tco...@gmail.com> wrote in message
news:1171054883....@a34g2000cwb.googlegroups.com...