exception

Exception handling: Contract vs Exceptional approach

I know two approaches to Exception handling, lets have a look at them. Contract approach. When a method does not do what it says it will do in the method header, it will throw an exception. Thus the method "promises" that it will do the operation, and if it fails for some reason, it will throw an exception. Exceptional approach. ...

What is the general rule of thumbs for creating an Exception in Java?

I have been in both situations: Creating too much custom Exceptions Using too much the general Exception class In both cases the project started OK but soon became an overhead to maintain (and refactor). So what is the best practice regarding the creation of your own Exceptions? ...

When to choose checked and unchecked exceptions

In Java (or any other language with checked exceptions), when creating your own exception class, how do you decide whether it should be checked or unchecked? My instinct is to say that a checked exception would be called for in cases where the caller might be able to recover in some productive way, where as an unchecked exception would ...

Exception analysis tool for C++

I've been looking for a tool to extract exception information from a C++ program. The most wanted feature I'm looking for: I want to know all the exceptions that can be thrown from a function (which would include all the function that is called from that function recursively). I've always thought that documenting errors and exceptions i...

Why do my exception stack traces always point to the last method line?

I have a problem with my Visual Studio installation. When I got an exception I always have incorrect line numbers in it's stack trace. There are always point to last line of each method in my codebase. At the same time it's OK when I'm tracing programs with debugger. What's happed with PDBs? No, I'm not re-throwing exception at each met...

How do I extract the inner exception from a soap exception in ASP.NET?

I have a simple web service operation like this one: [WebMethod] public string HelloWorld() { throw new Exception("HelloWorldException"); return "Hello World"; } And then I have a client application that consumes the web service and then calls the operation. Obviously it will throw an exception :-) ...

How do I recover from an unchecked exception?

Unchecked exceptions are alright if you want to handle every failure the same way, for example by logging it and skipping to the next request, displaying a message to the user and handling the next event, etc. If this is my use case, all I have to do is catch some general exception type at a high level in my system, and handle everything...

Why is .NET exception not caught by try/catch block?

I'm working on a project using the ANTLR parser library for C#. I've built a grammar to parse some text and it works well. However, when the parser comes across an illegal or unexpected token, it throws one of many exceptions. The problem is that in some cases (not all) that my try/catch block won't catch it and instead stops executio...

Finding out the source of an exception in C++ after it is caught?

I'm looking for an answer in MS VC++. When debugging a large C++ application, which unfortunately has a very extensive usage of C++ exceptions. Sometimes I catch an exception a little later than I actually want. Example in pseudo code: FunctionB() { ... throw e; ... } FunctionA() { ... FunctionB() ... } try...

How to Catch an exception in a using block with .NET 2.0?

I'm trying to leverage the using block more and more these days when I have an object that implements IDisposable but one thing I have not figured out is how to catch an exception as I would in a normal try/catch/finally ... any code samples to point me in the right direction? Edit: The question was modified after reading through the re...

How can I assert() without using abort()?

If I use assert() and the assertion fails then assert() will call abort(), ending the running program abruptly. I can't afford that in my production code. Is there a way to assert in runtime yet be able to catch failed assertions so I have the chance to handle them gracefully? ...

Portably handle exceptional errors in C++

I'm working on porting a Visual C++ application to GCC (should build on MingW and Linux). The existing code uses __try { ... } __except(1) { ... } blocks in a few places so that almost nothing (short of maybe out of memory type errors?) would make the program exit without doing some minimal logging. What are the options for doing somet...

Database exception handling best practices

How do you handle database exceptions in your application? Are you trying to validate data prior passing it to DB or just relying on DB schema validation logic? Do you try to recover from some kind of DB errors (e.g. timeouts)? Here are some approaches: Validate data prior passing it to DB Left validation to DB and handle DB exception...

Debugging an exception in an empty catch block

I'm debugging a production application that has a rash of empty catch blocks sigh: try {*SOME CODE*} catch{} Is there a way of seeing what the exception is when the debugger hits the catch in the IDE? ...

Exceptions in Web Services

My group is developing a service-based (.NET WCF) application and we're trying to decide how to handle exceptions in our internal services. Should we throw exceptions? Return exceptions serialized as XML? Just return an error code? Keep in mind that the user will never see these exceptions, it's only for other parts of the applicatio...

Asking a Generic Method to Throw Specific Exception Type on FAIL

Right, I know I am totally going to look an idiot with this one, but my brain is just not kicking in to gear this morning. I want to have a method where I can say "if it goes bad, come back with this type of Exception", right? For example, something like (and this doesn't work): static ExType TestException<ExType>(string message) ...

Measuring exception handling overhead in C++

What is the best way to measure exception handling overhead/performance in C++? Please give standalone code samples. I'm targeting Microsoft Visual C++ 2008 and gcc. I need to get results from the following cases: Overhead when there are no try/catch blocks Overhead when there are try/catch blocks but exceptions are not thrown Overh...

Is there a good method in C# for throwing an exception on a given thread

The code that I want to write is like this: void MethodOnThreadA() { for (;;) { // Do stuff if (ErrorConditionMet) ThrowOnThread(threadB, new MyException(...)); } } void MethodOnThreadB() { try { for (;;) { // Do stuff } } catch (MyException ex)...

I need help with a MenuStrip Error

My users are having an intermittent error when using a Windows Forms application built in VB.NET 3.5. Apparently when they click on the form and the form re-paints, a red 'X' will be painted over the MenuStrip control and the app will crash with the following error. Has anyone seen this before? Can someone point me in the right direction...

Returning from a finally block in Java

I was surprised recently to find that it's possible to have a return statement in a finally block in Java. It seems like lots of people think it's a bad thing to do as described in 'Don't return in a finally clause'. Scratching a little deaper, I also found 'Java's return doesn't always' which shows some pretty horrible examples of othe...