Main() forced to handle exceptions

T

tcomer

Hello! I've run into a problem and I can't seem to understand "why"
the problem occurs. In my program:

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
 
J

John Duval

Hi tcomer,
I just ran your program and the exception *was* caught. Can you add
an exception handler to Main(), and write it out to the console or a
message box? My guess is that you're either getting a different
exception type (not FileNotFoundException), or you have some code in
the catch block of GetApplicationSettings which is also generating a
FileNotFoundException.

John
 
L

Laura T.

The documentation says that the Load method of XmlDocument class may throw
only XmlException, not FileNotFound exception or anything else. As you catch
only FileNotFound exception, your handler is not executed and the exception
will be blow as unhandled exception.

Are you sure you did see the FileNotFound exception?
 
S

Stoitcho Goutsev \(100\)

Load emthod does throw FileNotFound exception. It is true however that the
MSDN istalled on my local machine only lists the XmlException for the Load
method, but if you check online there is the full list of exceptions that
this method may throw
http://msdn2.microsoft.com/en-us/library/875kz807.aspx


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. said:
The documentation says that the Load method of XmlDocument class may throw
only XmlException, not FileNotFound exception or anything else. As you
catch only FileNotFound exception, your handler is not executed and the
exception will be blow as unhandled exception.

Are you sure you did see the FileNotFound exception?


tcomer said:
Hello! I've run into a problem and I can't seem to understand "why"
the problem occurs. In my program:

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
 
T

tcomer

In reply to: "are you sure that this is the exception that falls
through to the Main method?"

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.
 
B

Brian Gideon

Hello! I've run into a problem and I can't seem to understand "why"
the problem occurs. In my program:

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

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
 
T

tcomer

Reply To: "Take the code exactly as it is from your post and try
running it. Post back and let us know what happened."

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.
 
B

Brian Gideon

Reply To: "Take the code exactly as it is from your post and try
running it. Post back and let us know what happened."

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
 
S

Stoitcho Goutsev \(100\)

Are you getting this as a message in VS. This *first chance exception* means
that you run your application in the VS debugger and you have set the
debugger to stop on exceptions as they are thrown. The exception will be
cought later on; this is the debugger that shows this message. If you want
to turn off this feature go to the main menu - Debugger|Exceptions... and
remove the check marks under the *Thrown* column.
 
T

tcomer

Thanks to all of you for helping me solve the problem. As someone
stated in a previous post, changing the Debug | Exceptions options all
to User-unhandled only will fix the problem; the method now handles
the exception as it should. Again, thanks for all of your help!
 
S

Stoitcho Goutsev \(100\)

tcomer, even with this option your code should work correctly. That's why it
says "First chance exception..." because the debugger stops on the exception
as it is thrown and if you hit continue it will go through the normal code
and exception handling. This is just a debugger feature and has nothing to
do with the normal program's execution path.
 

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