473,473 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Main() forced to handle exceptions

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

Feb 9 '07 #1
10 1896
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" <tc****@gmail.comha scritto nel messaggio
news:11**********************@q2g2000cwa.googlegro ups.com...
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

Feb 9 '07 #2
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

On Feb 9, 9:50 am, "tcomer" <tco...@gmail.comwrote:
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

Feb 9 '07 #3
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." <LT@NOWHERE.COMwrote in message
news:ey**************@TK2MSFTNGP03.phx.gbl...
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" <tc****@gmail.comha scritto nel messaggio
news:11**********************@q2g2000cwa.googlegro ups.com...
>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


Feb 9 '07 #4
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.

Feb 9 '07 #5
On Feb 9, 8:50 am, "tcomer" <tco...@gmail.comwrote:
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

Feb 9 '07 #6
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.

Feb 9 '07 #7
On Feb 9, 12:14 pm, "tcomer" <tco...@gmail.comwrote:
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

Feb 9 '07 #8
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.
--
HTH
Stoitcho Goutsev (100)

"tcomer" <tc****@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
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.

Feb 9 '07 #9
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!
Feb 9 '07 #10
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.
--
HTH
Stoitcho Goutsev (100)

"tcomer" <tc****@gmail.comwrote in message
news:11*********************@a34g2000cwb.googlegro ups.com...
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!


Feb 12 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: - Steve - | last post by:
I have a situtation where if a overloaded operator is used incorrectly I need it to cout some info to the screen, end the function it was called in, and continue on in main. How on earth do you do...
17
by: ahaupt | last post by:
Hi all, I'm currently writing a load of class libraries, but not the main application iteslf. I want to provide some method for reporting errors back to the main application. At the moment...
5
by: | last post by:
Unfortunately I've used exception handling as a debugging tool. Now I want to be smarter about handling errors. Today in the global.asx in the Application_OnError event, I inserted code to email...
4
by: Boni | last post by:
Dear all, is it possible to handle 2 exception tipes in one block? I.e Try if XXX then
2
by: comp.lang.php | last post by:
if ($forceDownload) { // HANDLE FORCED DOWNLOAD $dlGen =& new DownloadGenerator($fullFilePath); $negativeIndex = $dlGen->generateForceDownloadHeaders(); $willDeleteTemp =...
2
by: Mahesh Devjibhai Dhola | last post by:
Hi, I have one program, where i am using IO and Socket Asynchronous methods "BeginXXX" and "EndXXX". Many time, it happens that in Async delegate method, some exception occurs and if i dont...
3
by: clintonb | last post by:
Some programmers, and even Microsoft documents, say you should only throw exceptions for exceptional situations. So how are others handling all the other unexceptional errors? How are you...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.