473,404 Members | 2,137 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,404 software developers and data experts.

Why is the exception in my delegate ignored?

When I compile and run this code outside of Visual Studio no exception
is reported to the console and the program continues to run.

class Program
{

static void Main(string[] args)
{
System.Timers.Timer t = new System.Timers.Timer(10);
t.Elapsed += delegate { throw new Exception("Kaboom"); };
t.Start();

Console.ReadLine();
}
}

Any ideas?

Thanks,
James

Apr 2 '07 #1
4 1546
On Mon, 02 Apr 2007 09:47:59 -0700, james <ja********@gmail.comwrote:
When I compile and run this code outside of Visual Studio no exception
is reported to the console and the program continues to run.
As far as I know, that's "by design". That is, since you have no
exception handler in the new thread, there's no code to detect the
exception and so the thread just exits (abnormally) without notice.

Pete
Apr 2 '07 #2
Thanks Pete. Along with that would anyone mind telling me the best
way to have that exception blow up my application? Is there a way
that would work with AppDomain.CurrDomain.UnhandledException?

Thanks,
James

Apr 2 '07 #3
Peter Duniho <Np*********@nnowslpianmk.comwrote:
On Mon, 02 Apr 2007 09:47:59 -0700, james <ja********@gmail.comwrote:
When I compile and run this code outside of Visual Studio no exception
is reported to the console and the program continues to run.

As far as I know, that's "by design". That is, since you have no
exception handler in the new thread, there's no code to detect the
exception and so the thread just exits (abnormally) without notice.
The fact that it's running from a timer is significant though. In .NET
2.0, if a new "plain" thread throws an exception, it will abort the
program. Try this:

using System;

class Program
{
static void Main(string[] args)
{

new System.Threading.Thread(Foo).Start();

Console.ReadLine();
}

static void Foo()
{
throw new Exception();
}
}

In .NET 1.1 (IIRC) this wouldn't stop the program, but in .NET 2.0 it
does. From a *very* quick bit of experimentation, it looks like
threadpool threads throwing exceptions doesn't cause the program to
terminate, and System.Timers.Timer runs on thread pool threads. That
could be an over-simplification though...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 2 '07 #4
Hello James,

Yep, you can use this (or Application.ThreadException for winforms)
Take into account that not all unhandled exceptions in threads terminate
thread and shutdown hosting process - ThreadAbortException is out of this
fule, afaik

---
WBR, Michael Nemtsev [C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

JThanks Pete. Along with that would anyone mind telling me the best
Jway to have that exception blow up my application? Is there a way
Jthat would work with AppDomain.CurrDomain.UnhandledException?
J>
JThanks,
JJames
Apr 2 '07 #5

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

Similar topics

6
by: Sharon | last post by:
How to catch an asynchronous delegate invocation exception? The code: --------------------------------------- String message = “my message”; foreach( MyDelegate handler in...
16
by: ChInKPoInt [No MCSD] | last post by:
I am using Visual Studio 2K3 writing a ASP.NET web application. Is there a way to force the C# compiler to catch possible exception? In Java, all exceptions thrown MUST BE caught, otherwise...
2
by: Natalia DeBow | last post by:
Hi there, I am working on an client-server app, where the client asynchronously issues a request for the server to perform some action and the server is supposed to notify the client when the...
44
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...
1
by: ng5000 | last post by:
Hi, I have a C# application. The application starts a thread. I have a method that is run in the context of the thread. Sometimes (depends how it's called etc.) the method catches, logs, and...
9
by: Sharon | last post by:
I have a remoting object, lets call it A, that fires an event to remotely registered handlers, lets call it B. The invocation is working OK and the remote handler B is handling the event like it...
16
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following...
0
by: mike.varley1 | last post by:
Hi All We are getting the warning messages below several times a day in the app event logs on our Win2k3 server. Can anyone shed any light on the meaning\cause of the DC issue, especially: "This...
2
by: james | last post by:
I have a console app with the code: public static void Main(String args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( delegate(object sender,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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 projectplanning, 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.