473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Exception Handling + Winforms

Ori
Hi,

I would like to create some mechanism to handle all the exceptions
which will happen in my application.

I have one form which is the base form while all the other forms
inherit from it.

I try to find a way to "register" the base form to the Error handling
, but I don't know how….

Basically I'm try to catch an exceptions which occur in form2 (which
inherit from form1) through and exception handling which I will
implement through form1.

If any one has any idea, please let me know.

Thanks,

Ori.
Nov 15 '05 #1
7 17701
Ori,
Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

Hope this helps
Jay

"Ori" <or*******@hotmail.com> wrote in message
news:b4*************************@posting.google.co m...
Hi,

I would like to create some mechanism to handle all the exceptions
which will happen in my application.

I have one form which is the base form while all the other forms
inherit from it.

I try to find a way to "register" the base form to the Error handling
, but I don't know how..

Basically I'm try to catch an exceptions which occur in form2 (which
inherit from form1) through and exception handling which I will
implement through form1.

If any one has any idea, please let me know.

Thanks,

Ori.

Nov 15 '05 #2
Check out the Application.ThreadException
http://tinyurl.com/2upm9

Remarks
This event allows your Windows Forms application to handle otherwise
unhandled exceptions. Attach your event handlers to the ThreadException
event to deal with these exceptions, as these exceptions will leave your
application in an unknown state. Where possible, exceptions should be
handled by a structured exception handling block.
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

"Ori" <or*******@hotmail.com> schreef in bericht
news:b4*************************@posting.google.co m...
Hi,

I would like to create some mechanism to handle all the exceptions
which will happen in my application.

I have one form which is the base form while all the other forms
inherit from it.

I try to find a way to "register" the base form to the Error handling
, but I don't know how..

Basically I'm try to catch an exceptions which occur in form2 (which
inherit from form1) through and exception handling which I will
implement through form1.

If any one has any idea, please let me know.

Thanks,

Ori.

Nov 15 '05 #3
Hello Jay,
For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.
Is this event only for console applications? I thought it is raised despite
the type of the application (well, ASP .NET ones can be an exception). The
docs seem to say nothing about WinForms or console applications in the
"AppDomain.UnhandledException" topic.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Ori,
Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

Hope this helps
Jay

"Ori" <or*******@hotmail.com> wrote in message
news:b4*************************@posting.google.co m...
Hi,

I would like to create some mechanism to handle all the exceptions
which will happen in my application.

I have one form which is the base form while all the other forms
inherit from it.

I try to find a way to "register" the base form to the Error handling
, but I don't know how..

Basically I'm try to catch an exceptions which occur in form2 (which
inherit from form1) through and exception handling which I will
implement through form1.

If any one has any idea, please let me know.

Thanks,

Ori.



Nov 15 '05 #4
Dmitriy,
Yes its raised on all three, however it is the only one raised for console
applications, the other two are not.

My understanding of the event itself, which I admit is limited, its
usefulness in ASP.NET & Windows Form is "limited", hence my statement "for
console application". As I personally do not have a clear indication of when
you would need to include it in ASP.NET & Windows Forms, at least I cannot
really explain it. If you have something the clearly & briefly explains when
the event is raised, I would love to hear it.

Remember most unhandled exceptions in most Windows Forms apps will be
handled by the Application.ThreadException, the AppDomain.UnhandledException
will only handle the exceptions not handled by the above handler... (at
least that is my experience).

Hope this helps
Jay

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:O9**************@TK2MSFTNGP11.phx.gbl...
Hello Jay,
For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.
Is this event only for console applications? I thought it is raised

despite the type of the application (well, ASP .NET ones can be an exception). The
docs seem to say nothing about WinForms or console applications in the
"AppDomain.UnhandledException" topic.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ori,
Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

Hope this helps
Jay

"Ori" <or*******@hotmail.com> wrote in message
news:b4*************************@posting.google.co m...
Hi,

I would like to create some mechanism to handle all the exceptions
which will happen in my application.

I have one form which is the base form while all the other forms
inherit from it.

I try to find a way to "register" the base form to the Error handling
, but I don't know how..

Basically I'm try to catch an exceptions which occur in form2 (which
inherit from form1) through and exception handling which I will
implement through form1.

If any one has any idea, please let me know.

Thanks,

Ori.


Nov 15 '05 #5
> Remember most unhandled exceptions in most Windows Forms apps will be
handled by the Application.ThreadException, the AppDomain.UnhandledException will only handle the exceptions not handled by the above handler... (at
least that is my experience).
Yes, I have bumped into something similar when working on a VS .NET add-in.
Strangely enough, the UnhandledException event didn't raise for the primary
thread the add-in was running on, while the event was raised as expected for
any other thread I had created manually. This came as a big and unpleasant
surprise, and I still haven't got any 'official' explanation. My only guess
is since VS .NET communicates with the add-in through CCW (COM-callable
wrappers), it is possible the framework silently installs its own handler
for the UnhandledException event to translate a managed exception to a COM
error (HRESULT + IErrorInfo).

As for the WinForms and ASP .NET applications, it is indeed better to use
the recommended means such as Application.ThreadException. Still, I'd add a
handler for UnhandledException too not to miss "survivors" who managed to
bypass the "first line of defense".

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Dmitriy,
Yes its raised on all three, however it is the only one raised for console
applications, the other two are not.

My understanding of the event itself, which I admit is limited, its
usefulness in ASP.NET & Windows Form is "limited", hence my statement "for
console application". As I personally do not have a clear indication of when you would need to include it in ASP.NET & Windows Forms, at least I cannot
really explain it. If you have something the clearly & briefly explains when the event is raised, I would love to hear it.

Remember most unhandled exceptions in most Windows Forms apps will be
handled by the Application.ThreadException, the AppDomain.UnhandledException will only handle the exceptions not handled by the above handler... (at
least that is my experience).

Hope this helps
Jay


Nov 15 '05 #6
Dmitriy,
Still, I'd add a
handler for UnhandledException too not to miss "survivors" who managed to
bypass the "first line of defense". You won't get an argument for me, I was attempting the highlight just the
first line of defense, in an effort to "keep it simple".

When I use my exception snippet again I will try to incorporate our
discussion...

Thanks
Jay

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:eS**************@TK2MSFTNGP11.phx.gbl...
Remember most unhandled exceptions in most Windows Forms apps will be
handled by the Application.ThreadException, the

AppDomain.UnhandledException
will only handle the exceptions not handled by the above handler... (at
least that is my experience).


Yes, I have bumped into something similar when working on a VS .NET

add-in. Strangely enough, the UnhandledException event didn't raise for the primary thread the add-in was running on, while the event was raised as expected for any other thread I had created manually. This came as a big and unpleasant
surprise, and I still haven't got any 'official' explanation. My only guess is since VS .NET communicates with the add-in through CCW (COM-callable
wrappers), it is possible the framework silently installs its own handler
for the UnhandledException event to translate a managed exception to a COM
error (HRESULT + IErrorInfo).

As for the WinForms and ASP .NET applications, it is indeed better to use
the recommended means such as Application.ThreadException. Still, I'd add a handler for UnhandledException too not to miss "survivors" who managed to
bypass the "first line of defense".

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Dmitriy,
Yes its raised on all three, however it is the only one raised for console applications, the other two are not.

My understanding of the event itself, which I admit is limited, its
usefulness in ASP.NET & Windows Form is "limited", hence my statement "for console application". As I personally do not have a clear indication of

when
you would need to include it in ASP.NET & Windows Forms, at least I cannot really explain it. If you have something the clearly & briefly explains

when
the event is raised, I would love to hear it.

Remember most unhandled exceptions in most Windows Forms apps will be
handled by the Application.ThreadException, the

AppDomain.UnhandledException
will only handle the exceptions not handled by the above handler... (at
least that is my experience).

Hope this helps
Jay

Nov 15 '05 #7
Jay,

I didn't mean to argue either, my sole intention was to warn you and the
community that peculiar things with UnhandledException could happen and they
were undocumented.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
Dmitriy,
Still, I'd add a
handler for UnhandledException too not to miss "survivors" who managed to
bypass the "first line of defense". You won't get an argument for me, I was attempting the highlight just the
first line of defense, in an effort to "keep it simple".

When I use my exception snippet again I will try to incorporate our
discussion...

Thanks
Jay

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:eS**************@TK2MSFTNGP11.phx.gbl... Remember most unhandled exceptions in most Windows Forms apps will be
handled by the Application.ThreadException, the

AppDomain.UnhandledException
will only handle the exceptions not handled by the above handler... (at least that is my experience).


Yes, I have bumped into something similar when working on a VS .NET

add-in.
Strangely enough, the UnhandledException event didn't raise for the

primary
thread the add-in was running on, while the event was raised as expected

for
any other thread I had created manually. This came as a big and unpleasant
surprise, and I still haven't got any 'official' explanation. My only

guess
is since VS .NET communicates with the add-in through CCW (COM-callable
wrappers), it is possible the framework silently installs its own handler for the UnhandledException event to translate a managed exception to a COM error (HRESULT + IErrorInfo).

As for the WinForms and ASP .NET applications, it is indeed better to use the recommended means such as Application.ThreadException. Still, I'd add a
handler for UnhandledException too not to miss "survivors" who managed

to bypass the "first line of defense".

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Dmitriy,
Yes its raised on all three, however it is the only one raised for console applications, the other two are not.

My understanding of the event itself, which I admit is limited, its
usefulness in ASP.NET & Windows Form is "limited", hence my statement "for console application". As I personally do not have a clear indication of when
you would need to include it in ASP.NET & Windows Forms, at least I cannot really explain it. If you have something the clearly & briefly
explains when
the event is raised, I would love to hear it.

Remember most unhandled exceptions in most Windows Forms apps will be
handled by the Application.ThreadException, the

AppDomain.UnhandledException
will only handle the exceptions not handled by the above handler...

(at least that is my experience).

Hope this helps
Jay



Nov 15 '05 #8

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

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
1
by: Jon | last post by:
Can someone point me to a best practices for exception handling in .Net 2? We have a method that we like for ASP.Net apps on .Net 1.1, but are just moving to 2.0 with winforms apps. Is it still...
6
by: Robin Riley | last post by:
Hi, I have a .NET solution that contains a dll project and a tester application project which, of course, invokes the dll. The dll project has exception handling in it. What's happening is that...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.