exception-handling

Exception Handling (Task Parallel Library) .Net Framework 4.0 Beta 2

Hi everybody, at the moment I am trying some of the new Features of the Task Parallel Library, shipped with the .Net Framework 4.0 Beta 2. My question relates specifically to the Exception Handling within the TPL as described here: http://msdn.microsoft.com/en-us/library/dd997415%28VS.100%29.aspx First example (changed it a little bi...

C++ Exception Design Pattern

Hello all :) I'd like to encapsulate Win32 errors (those returned from GetLastError()) in some form of exception class. Rather than having a single Win32 exception, however, I'd like to be able to have a specialized exception catchable for common errors, such as ERROR_ACCESS_DENIED. For example, I'd have classes declared like this: cl...

How to add global exception handling to a add-in dll?

Here's my context: I am writing a WPF add-in for an application. This Application's main thread is unmanaged. I want to add a global exception handling system for this add-in to handle any unhandled exceptions. Here's what I've tried but not working: I cannot add a try-catch block to my Application.Run() code line. Because I am an ...

What's the use of a finally block preceded by a catch-all catch block, in C#?

Consider the following C# code structure (S0-S3 are placeholders for arbitrary code blocks): try { S0; } catch (Exception ex) { S1; } finally { S2; } S3; In the case that S1 throws an exception inside the catch handler, S2 inside the finally will still execute (but S3 will not). Question Assuming that S1 cannot throw, i...

When catching a general exception, how can I determine the original exception type?

When catching an exception in .net, you can have as many type-specific exception blocks as needed. But I usually try to have at least one "general" exception catch block. But is there a way to get the type of the "real" exception thrown that is caught by the generic exception handler, perhaps using reflection? For example, if I have C...

What are some implementations of Exception filtering in the Circuit Breaker pattern?

The Circuit Breaker pattern, from the book Release It!, protects a remote service from requests while it is failing (or recovering) and helps the client manage repeated remote service failure. I like Davy Brion’s stateful circuit breaker and Ayende’s lazy timeout fix is very clean. However, I have not seen a lot of implementations of fi...

Lazy timeout resolution Circuit Breaker is throwing ArgumentOutOfRangeException.

Ayende posted a modification to Davy Brion's circuit breaker, wherein he changed the timeout resolution to a lazy model. private readonly DateTime expiry; public OpenState(CircuitBreaker outer) : base(outer) { expiry = DateTime.UtcNow + outer.timeout; } public override void ProtectedCodeIsAboutToBeCalled() { if(DateTime.Ut...

Refactoring to remove try/catch

Any ideas on a good way to refactor this so that my code acts the same, but without the whole throwing and catching my own exception? public Int32 ChooseNextColor(Int32 numColors) { int? nextColor = null; while (nextColor == null) { Console.Write("Please enter your next color selection: "); string input = Co...

How to catch an exception in python and get a reference to the exception, WITHOUT knowing the type?

I'm wondering how I can catch any raised object (i.e. a type that does not extend Exception), and still get a reference to it. I came across the desire to do this when using Jython. When calling a Java method, if that method raises an exception, it will not extend Python's Exception class, so a block like this will not catch it: try:...

Exception Handling Dilemma

Working on existing code, I recently found smt like this: if(Config.ExceptionBehavior.DEBUG_EXCEPTIONS) { foo.Foo(); } else { try { foo.Foo(); } catch(Exception ex) { //whatever } } I am getting rid of this - but I do see some value in the driver of this kind of code: basically the guy who wrote this w...

C#: Can I get the details from an Unhandled Exception from the CLR?

First, I'm sorry for what must be a duplicate post. I cannot seem to narrow my search down enough on SO and google to find what I'm looking for. I'm currently working in a shop where they like their exceptions to bubble up. When the compiled app goes to test, it's a big pain trying to get the details of the exception (message and stac...

Python function local variable scope during exceptions

Background: I'm doing COM programming of National Instruments' TestStand in Python. TestStand complains if objects aren't "released" properly (it pops up an "objects not released properly" debug dialog box). The way to release the TestStand COM objects in Python is to ensure all variables no longer contain the object—e.g. del() them, or ...

how to know a Java Socket is dead

I use a Java Socket object in my client application. I need to know when the line to the server is broken, or if any event caused the socket to be dead. I see two methods: catching SocketException when writing in or reading from the socket, considering these exceptions kill the socket when catching these exceptions, checking the Socke...

What is proper code pattern to finalize transactions in Java (rollback on exception and commit on success)?

I'm looking for generic code pattern to properly handle transaction with respect to possible exception. I assume there is common code pattern for that, no matter what concrete kind of transaction we are dealing with. I have a method that executes something in transaction and want to rethrow Exception that may occur when inside transacti...

Memory exception while XDocument.Save()

I am trying to save an XDcoument to a thumb drive which doesnt have enough memory space available. (This is a special test condition for the app) Though the application is giving an exception like below, I cant get that in the try catch block around the XDocument.Save(filePath). Looks like it is a delayed throw. Is it a LINQ issue or am ...

Get thrown exception in finally block.

Is there a way, how to get currently thrown exception (if exists)? I would like reduce amount of code and apply some reuse for task looks like: Exception thrownException = null; try { // some code with 3rd party classes, which can throw unexpected exceptions } catch( Exception exc ) { thrownException = exc; LogException( ex...

How to check that all exceptions thrown have their matching catch clause

In java the compiler complains about uncatched exceptions. I use exceptions in C++ and I miss that feature. Is there a tool out there capable of doing it? maybe a compiler option (but I doubt it) ...

MSTest Test Context Exception Handling

Is there a way that I can get to the exception that was handled by the MSTest framework using the TestContext or some other method on a base test class? If an unhandled exception occurs in one of my tests, I'd like to spin through all the items in the exception.Data dictionary and display them to the test result to help me figure out wh...

Redirect asp.net mvc if custom exception is thrown...

I need to globally redirect my users if a custom error is thrown in my application. I have tried putting some logic into my global.asax file to search for my custom error and if it's thrown, perform a redirect, but my application never hits my global.asax method. It keeps giving me an error that says my exception was unhandled by user ...

Try/catch in Java

Could someone please give me a hint why this try and catch is not working? It throws a scanner exception instead of printing the message I expect. import java.util.*; import java.io.*; import java.math.*; import javax.swing.*; public class Main { public static void main(String[] args) { Boolean test = true; while (t...