exception-handling

Best way to check whether a certain exception type was the cause (of a cause, etc ...) in a nested exception?

I am writing some JUnit tests that verify that an exception of type MyCustomException is thrown. However, this exception is wrapped in other exceptions a number of times, e.g. in an InvocationTargetException, which in turn is wrapped in a RuntimeException. What's the best way to determine whether MyCustomException somehow caused the exc...

How to implement top level exception handling?

I recently had to develop an additional module for an existing service developed by a colleague. He had placed a try/catch block in the main working function for catching all unhadled exceptions that bubbled up to this level, logging them together with stack trace info etc: try { // do main work } catch(Exception ex) { // log ex...

Stack unwinding in case of structured exceptions

This question provides more clarity on the problem described here. I did some more investigation and found that the stack unwinding is not happening in the following piece of code: class One { public: int x ; }; class Wrapper { public: Wrapper(CString csText):mcsText(csText) { CString csTempText; csTempText.Format...

How to handle general exceptions in Asp.Net MVC?

I want to transfer all unhandled exceptions to an error page in Asp.Net MVC. What is the way to handle the unhandled exceptions in Asp.net MVC? Is there anything like application_error? ...

ASP.net MVC [HandleError] not catching exceptions.

In two different application, one a custom the other the sample MVC application you get with a new VS2008 MVC project, [HandleError] is not catching exceptions. In the sample application I have: [HandleError] public class HomeController : Controller { public ActionResult Index() { ViewData["Message"] = "Welcome to ASP....

Is there a way in C# to catch all exceptions from a Control/Form?

I'd like to have something like a "catch-all" for exceptions thrown from a inside a Control or a Form that reside within the current thread, but without resorting to using the Application.ThreadException of the entire thread. Why? Because I'd like to keep the exception handling modular. All the code that can cause the problems is being...

Testing Last Chance Exception Handling

I am trying to write a unit test to cover code found in our "last chance exception handler". When referring to last chance exception handling I am talking about event handlers of the events: Application.ThreadException AppDomain.CurrentDomain.UnhandledException More or less I am verifying that logs are being produced detailing the inf...

Is there a reason to NOT use .NET Framework-defined exception classes in your own code?

So, we are using the Enterprise Library 4.1 Exception Handling Application Block to deal with logging/handling our exceptions in a multiple-project application. We have a few custom exceptions and are throwing some exceptions whose classes are defined in the .NET framework's standard class libraries (e.g. ArgumentException, InvalidOperat...

Is it ok to rely on a try-catch in a CreateOrUpdate method for the Entity Framework?

Is it acceptable to do this? First try to the add the entity. If the add fails, it doesn't matter because that means the entity already exists? Or is there a more elegant/easy solution? EntityFrameworkEntities dal = EntityDataModelHelper.GetEntityDataModel(); try { dal.AddToXXXXXX(xxxxxxx); } catch { } try { dal.SaveChange...

Python - test that succeeds when exception is not raised

I know about unittest Python module. I know about assertRaises() method of TestCase class. I would like to write a test that succeeds when an exception is not raised. Any hints please? ...

why does throw "nothing" causes program termination?

const int MIN_NUMBER = 4; class Temp { public: Temp(int x) : X(x) { } bool getX() const { try { if( X < MIN_NUMBER) { //By mistake throwing any specific exception was missed out //Program terminated here throw ; } } catch (bool bTemp) { cout<<"cat...

Exception handling problem

I'm trying to write some code that catches a particular exception and throw a more useful one for something higher up the call stack to deal with but also catch more general exceptions and handle them. The code is something like this: try { // Do stuff } catch (SomeException e) { throw new SomeExceptionWithContextInfo(); } catc...

Can you catch exception from inside Application.DoEvents()?

I've encountered a strange difference between a program running in VS2005 and running the executable directly. Essentially, when an exception is thrown in a method inside an Application.DoEvents() call, the exception can be caught when running inside Visual Studio. When running the compiled executable, the exception is not caught and t...

Enterprise Library Exception Handling Application Block and Logging Application Block proper use in ASP.NET

I have a grasp on this application block with ASP.NET, but I feel unsure if I am coding with it properly in ASP.NET. I've looked all over for proper examples of how to use the Enterprise Library Exception Handling Application Block, but only turn up articles for Windows Forms. Could somebody please point me in the right direction for u...

EntLib 4.0: Exception Handling Application Block

I'm just starting to use the Enterprise Library Exception Handling block. It seems a little cumbersome. Do I really have to do try { //Do something with a DirectoryInfo object } catch(DirectoryNotFoundException ex) { bool rethrow = ExceptionPolicy.Handle(ex, _exceptionPolicyName); if(rethrow) throw; } Eve...

Tools to use to find suppressed exceptions?

I work on a fairly complex Open Source project (opensimulator.org). It's currently sitting around 300 KLOC of C#, and there are a number of places where code has built up to trap and ignore exceptions, which end up disguising subtle bugs. I'm wondering what tools are out there that can produce reports of overly general exception catchin...

Would you ever NOT catch an exception, or throw an exception that won't be caught?

I've dealt with instances where I would throw/rethrow an exception knowing that the code surrounding it would catch the specific exception. But is there any time you would want to throw an exception, knowing that it wouldn't be caught? Or at least, NOT catch an exception? Exceptions immediately halt the application unless their handle...

C++: Cross thread exception handling problem with boost::exception

Basically, I've got a situation where one thread throws an exception which a different thread needs to handle. I'm trying to do this with boost exception, however somewhere along the line the exception loses its type and thus isn't caught by the catch blocks. Basically, thread B wants to do something, however for various reasons it mus...

How do you add context to exceptions without tons of boilerplate code?

In my current project I see a lot of this type of code: public User GetUserByName(string userName) { try { // Lots of code here to check if the user is in the cache, // get it from the DB if not, set properties on it, etc... } catch (Exception ex) { throw new Exception("Exception getting user...

How to display errors to the user while still logging it?

I'm using a PyQt4 user interface. I've redirected stderr to a log file for easy debugging and trouble-shooting, but now I need to display error messages to the user when an error occurs. My issue is that I need to catch an exception when it happens and let the user know that it happened, but still let the traceback propagate to stderr ...