473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exception handling



Hi

i have setup a global exception handler b4 Application.Run like
Application.Thr eadException += new
ThreadException EventHandler(Gl obalExceptionPr ocessing.AppThr eadException
);

then after Application.Run (new Form1()); i have setup a global keyboard
hook

my problem is that if an error occured inside keyboardHookPro c the
global exception error does not catch it

am i missing something?

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
13 2307
How about setting up a similar event handler for
AppDomain.Unhan dledException ?

Nov 17 '05 #2
i though of that but could not find a way to make my application not die
after handling that event. Is it possible?

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Why in the world would you want to set up a global exception handler?
You should catch exceptions relating to what failed, I don't think
you'll be able to effectively deal with thrown exceptions if you use
some one-size-fits-all technique common to old-school programming.

You can also "assume" that a good portion of the opcode will work, while
code that relies on unknown entities like remote servers etc, may
fail, and those you surround in try/catch... like

try
{
///myunknown routine here
}

catch (Exception ex)
{
Messagebox.Show (ex.message);
}

....like that. You shouldnt be using a single event handler for any
exception that gets thrown in the module.

tolisss wrote:

Hi

i have setup a global exception handler b4 Application.Run like
Application.Thr eadException += new
ThreadException EventHandler(Gl obalExceptionPr ocessing.AppThr eadException
);

then after Application.Run (new Form1()); i have setup a global keyboard
hook

my problem is that if an error occured inside keyboardHookPro c the
global exception error does not catch it

am i missing something?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
Hi,

While I cannot give you an answer of why it happen I can give you ideas of
how to workaround it.

What if you use a try/catch block inside the hook, if you detect an
exception you could call GlobalException Processing.AppT hreadException
( I think it's a static method , right ? )
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"tolisss" <no****@devdex. com> wrote in message
news:Ov******** ******@TK2MSFTN GP14.phx.gbl...


Hi

i have setup a global exception handler b4 Application.Run like
Application.Thr eadException += new
ThreadException EventHandler(Gl obalExceptionPr ocessing.AppThr eadException
);

then after Application.Run (new Form1()); i have setup a global keyboard
hook

my problem is that if an error occured inside keyboardHookPro c the
global exception error does not catch it

am i missing something?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5
In order to have your application "not die," you must catch events
specifically where they happen and handle them there. Once an exception
has made it up the stack to a global event handler like
Thread.ThreadEx ception or AppDomain.Unhan dledException, there is no way
to indicate that you want to "go back" to where the exception
originally occurred and continue execution from that point.

All that these global event handlers do for you is give you the
opportunity to handle the unhandled exceptions in your own way before
your application dies, rather than settling for the default behaviour:
for example, inserting code to log the exceptions to a file. They don't
give you the opportunity to control where your application continues
execution, so far as I know.

Nov 17 '05 #6
Hi,

AFAIK there is no way to prevent the application from finishing. All you can
know is IF the application is finishing or not , IIRC if it's a worker
thread the app does not ends.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"tolisss" <no****@devdex. com> wrote in message
news:OR******** ******@tk2msftn gp13.phx.gbl...
i though of that but could not find a way to make my application not die
after handling that event. Is it possible?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #7
> Why in the world would you want to set up a global exception handler?
You should catch exceptions relating to what failed...

Absolutely not. You should catch exceptions that you know how to deal
with in your local code. For example, if you're trying to convert a
string to a double, and you know it might fail, but you have a default
value, you should catch the exception, set the default value, and allow
execution to continue.

Another example: if you try to call a Web Service and the call fails,
and that particular call isn't critical to your application, you could
catch the exception, warn the user that some information is not
available, and continue execution.

Global event handlers are for all of those other exceptions: the ones
that you can't handle gracefully... the ones that will kill your
application. A global event handler at least lets you tell the user
nicely that something has gone horribly wrong and you have to shut down
now. For example, we use them to display what we want the user to see
(in a MessageBox) but log the gory details to a log file for analysis
by the IS department.

Again, these are exceptions for which the application has no recovery
strategy, and so has to shut down. You should allow them to go
unhandled, and catch them in the global exception handler in order to
do user-friendly things in such a failure situation.
try
{
///myunknown routine here
}
catch (Exception ex)
{
Messagebox.Show (ex.message);
}


This is wrong on two counts. First and most aggrevious, it shows an
exception message to the user (is that going to mean anything to the
user?) and then _merrily carries on_ executing the program, even
thought the "myunknownrouti ne" _failed_ with an exception! How do you
know that the application _can_ continue functioning if that routine
fails? Is the state of the application such that it can survive that?
Or is everything in an indeterminate state now, a state that will
result in (at best) further exceptions or (at worst) subtle corruption
of your company's database?

The only way you should ever do this is if you _know_ that
"myunknownrouti ne" really isn't all that important, and if it fails
it's no big deal. In that case, yes, you should catch the exception,
and either silently log it to a file somewhere, or log it to a file and
warn the user that things are less than optimal, but the program can
still keep working.

The second reason that this is wrong is that you should avoid
catch-alls like "catch (Exception ex)". Carrying on with the idea that
you should only catch exceptions that you know are no big deal, do you
really know that _every exception_ that "myunknownrouti ne" could
possibly throw is no big deal? What about OutOfMemoryExce ption? Do you
really want to catch that one and continue as though nothing happened?
What about InvalidArgument Exception, which indicates an error in your
calling code? What if you verify that every exception is, in fact, no
big deal, but then the next revision of the library adds a new
exception? In general, you should catch _specific exceptions_ that you
know you can deal with, and let the other ones go.

There is one situation I've found, though, in which "catch (Exception
ex)" is inevitable. That's when a vendor's documentation about what
exceptions a method throws is lacking. (See the MSDN documentation on
the Crystal .NET stuff for an example of crap doc that doesn't tell you
_anything_ about exceptions.) In cases like this, you really have no
idea which exceptions a method will throw, so you either have to run
some tests and figure out which exceptions you can handle, or decide
that it's no big deal if the method fails and just do "catch (Exception
ex)". I, for example, use this catch-all and just inform the user that
the report couldn't be printed, and then move on. That's one case in
which a method failure is not a problem.

Global exception handlers are not "old-school programming": they are
the recommended way of gracefully shutting down an application that has
failed.

Nov 17 '05 #8
Bruce Wood wrote:
Absolutely not. You should catch exceptions that you know how to deal
with in your local code. For example, if you're trying to convert a
string to a double, and you know it might fail, but you have a default
value, you should catch the exception, set the default value, and allow
execution to continue.


Dealing with say, an unavailable resource is a lot like the application
not working at all if it depends on that resource... we catch what we
think may give us an error...

I don't have anything against a clean exit if there is an error that we
don't know what to do with, but I question how the framework may handle
that exception by default... an error message from the framework with
MSIL may not be much worse than a custom error message based on the
thrown exception we have caught.
Nov 17 '05 #9
Bruce Wood wrote:
Absolutely not. You should catch exceptions that you know how to deal
with in your local code. For example, if you're trying to convert a
string to a double, and you know it might fail, but you have a default
value, you should catch the exception, set the default value, and allow
execution to continue.


Dealing with say, an unavailable resource is a lot like the application
not working at all if it depends on that resource... we catch what we
think may give us an error...

I don't have anything against a clean exit if there is an error that we
don't know what to do with, but I question how the framework may handle
that exception by default... an error message from the framework with
MSIL may not be much worse than a custom error message based on the
thrown exception we have caught.
Nov 17 '05 #10

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

Similar topics

11
2867
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses return code (not generating error/exception) so it is more compatible with other programming language.
6
2334
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 assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't catch the exception and keep going. I'd just get the error message and then it would shut the...
7
5988
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2745
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 exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you please critique the following exception handling mechanism in terms of my requirements ?
2
2591
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access Layer. My questions is where should I put exception handling: 1) Should it be put in all significant methods in all layers? 2) Should I create an exception base class that will handle the errors and pass useful error messages to the user?
9
2533
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
44
4206
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
4
5129
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 .NET 2003\Vc7\include\xstring(1453) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
41
3045
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 handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out three handlers that all do the same thing. So, I could just catch Exception. But, is that...
1
3100
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
8688
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8352
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8494
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7178
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2614
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 we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.