exception-handling

Will code in a Finally statement fire if I return a value in a Try block?

I'm reviewing some code for a friend and say that he was using a return statement inside of a try-finally block. Does the code in the Finally section still fire even though the rest of the try block doesn't? Example: public bool someMethod() { try { return true; throw new Exception("test"); // doesn't seem to get executed ...

Why does the inner exception reach the ThreadException handler and not the actual thrown exception?

I'm seeing some wierd behaviour when throwing exceptions and catching them in the Application.ThreadException event handler. Basically whats happening in the sample below is that an exception is thrown in the DoWork event handler of a BackgroundWorker. The RunWorkerCompleted event handler rethrows a new exception with the original as th...

How to know the exact line of code where where an exception has been caused (C++)?

If I generate an exception by myself, I can include any info into the exception: a number of code line and name of source file. Something like this: throw std::exception("myFile.cpp:255"); But what's with unhandled exceptions or with exceptions that was generated not by me? Thanks. ...

Bad idea to chain exceptions with RMI?

Is it a bad idea to use exception chaining when throwing RemoteExceptions? We have an RMI server that does something like this: public Object doSomething() throws RemoteException { try { return getData(); } catch (CustomException ex) { throw new RemoteException(ex); } } I'm getting UnmarshallEx...

Exception Handling

Is Structured Exception Handling bad? What is the right way to handle exceptions? EDIT: Exception Handling in .NET using C#. I usually have a set of specific exception classes (DivideByZeroException, ArrayTypeMismatchException) and don't have a generic "catch (Exception ex)". The thinking behind this is that I expect certain types of ...

When to dodge exceptions and when to handle them

What guidlines do you use when deciding whether to let a method dodge an exception (ie: let the exception propogate up) or handle it once the exception is recieved? Here is an example of what I am trying to ask If I have three methods method1,2,3 and 3. Method1 calls Method2 which Calls Method3. and the exception is only thrown in meth...

Try Catch block

I have the following code Try 'Some code that causes exception Catch ex as ExceptionType1 'Handle Section - 1 Catch ex as ExceptionType2 'Handle section - 2 Catch ex as ExceptionType3 'Handle section - 3 Finally ' Clean up End Try Suppose ExceptionType1 is thrown by the code which is handled by section - 1. Aft...

What is the best way to collect crash data?

So I am sold on the concept of attempting to collect data automatically from a program - i.e., popping up a dialog box that asks the user to send the report when something goes wrong. I'm working in MS Visual Studio C#. From an implementation point of view, does it make sense to put a try/catch loop in my main program.cs file, around w...

Obtain a std::ostream either from std::cout or std::ofstream(file)

Hi, how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following: std::ostream out = condition ? &std::cout : std::ofstream(filename); I've seen some e...

Does it make sense to catch exceptions in the main(...) ?

Hi, I found some code in a project which looks like that : int main(int argc, char *argv[]) { // some stuff try { theApp.Run(); } catch (std::exception& exc) { cerr << exc.what() << std::endl; exit(EXIT_FAILURE); } return (EXIT_SUCCESS); } I don't understand why the exceptions are being catched. If they weren't, the app...

Throwing ArgumentNullException

Suppose I have a method that takes an object of some kind as an argument. Now say that if this method is passed a null argument, it's a fatal error and an exception should be thrown. Is it worth it for me to code something like this (keeping in mind this is a trivial example): void someMethod(SomeClass x) { if (x == null){ ...

Automated Exception Handling

I was wondering if something exists (in Java world) able to take an snapshot of the JVM current state with the following features: Do it while an exception is being thrown. Capture local variables, method's arguments, etc. Put it in a handy file which can be used to extract or reproduce in a IDE the situation in your source code. The...

If exception handling should be centralised is the design of WinForm components flawed?

Or any other design that has the gui widgets as being the first port of call where an unhandled exception will kill the app? We all want one main: "catch all" for face saving purposes (although in most cases this shouldn't have a "continue" feature) but it is impossible to easily implement one with gui widgets that are the first port of...

How to deal with exceptions

My technical lead insists on this exception mechanism: try { DoSth(); } catch (OurException) { throw; } catch (Exception ex) { Util.Log(ex.Message, "1242"); // 1242 is unique to this catch block throw new OurException(ex); } 1242 here is an identifier of the catch method which we handle an exception other than OurExcep...

Handling database exceptions in .net

When we can catch an exception like: Violation of UNIQUE KEY constraint 'IX_Product'. Cannot insert duplicate key in object 'Product'. (2627). The challenge is how to dechiper the Index Name IX_Product as a Member (i.e. I don't want to substring out the message). There could be more than one unique constraint on a table and we would n...

Should I check for DB constraints in code or should I catch exceptions thrown by DB

Hi, I have an application that saves data into a table called Jobs. The Jobs table has a column called Name which has a UNIQUE constraint. The Name column is not PRIMARY KEY. I wonder if I should check for duplicate entries myself before I try to save/update a new entry or if it's better to wait for an exception thrown by the data acces...

Microsoft Exception Handling Block - Isn't it a perfect example for overengineering?

Ever since Microsoft has introduced the application blocks, I've been bumping into people who use the Exception Handling Application Block. I've recently had a closer look myself and would summarize the basic functionality as follows (skip the following block if you already know what it does): The exception handling application block...

Catch MainLoop exceptions and displaying them in MessageDialogs

I have a wxPython application that relies on an external config file. I want provide friendly message dialogs that show up if there are any config errors. I've tried to make this work by wrapping my app.MainLoop() call in a try/except statement. The code below works for the init code in my MainWindow frame class, but doesn't catch any...

NotImplementedException - are they kidding me?

This really, really urks me, so I hope that someone can give me a reasonable justification for why things are as they are. NotImplementedException. You are pulling my leg, right? No, I'm not going to take the cheap stab at this by saying, "hang on, the method is implemented - it throws a NotImplementedException." Yes, that's right, y...

When to use assertion over exceptions in domain classes

Are there any situations when you would use assertion instead of exceptions-handling inside domain classes... ...