throw

How do exceptions work (behind the scenes) in c++

I keep seeing people say that exceptions are slow but I never see any proof. So instead of asking if they are I will ask how do exceptions work behind the scene so I can make a decisions of when to use them and if they are slow. From what I know exceptions are the same thing as doing a bunch of return but it also checks when it needs to...

Finally in C++

Is this a good way to implement a Finally-like behavior in standard C++? (Without special pointers) class Exception : public Exception { public: virtual bool isException() { return true; } }; class NoException : public Exception { public: bool isException() { return false; } }; Object *myObject = 0; try { // OBJECT CREATIO...

Exception vs Assert?

Is there a rule of thumb to follow when deciding to use exceptions instead of asserts (or vice versa). Right now I do only throw if its something I think will happen during runtime on the user side (like a socket or file error). Almost everything else I use asserts. Also, if I were to throw an assert, what is a nice standard object to t...

Throwing exceptions in Java

Hello, I have a question about throwing exceptions in Java, a kind of misunderstanding from my side, as it seems, which I would like to clarify for myself. I have been reading that the two basic ways of handling exception code are: 1.) throwing an exception in a try-block with "throw new ...", and catching it immediately in a catch-bl...

How to throw good exceptions?

I heard you should never throw a string because there is a lack of information and you'll catch exceptions you dont expect to catch. What are good practice for throwing exceptions? do you inherit a base exception class? Do you have many exceptions or few? do you do MyExceptionClass& or const MyExceptionClass& ? etc. Also i know exceptio...

What is "throw"

Hi All, Can anyone please explain me use of throw in exception handling? What happens when i throw an exception? ...

about c++ exceptions. func() throw()

i am reading this page http://www.cplusplus.com/doc/tutorial/exceptions.html it says if i write function() throw(); no exceptions can be thrown in that function. I tried in msvc 2005 writing throw(), throw(int), throw() and nothing at all. each had the exact same results. Nothing. I threw int, char*, another type and it was all caught th...

What is the benefit to limiting throws allowed by a C++ function?

What is the benefit of declaring the possible exception-throws from a C++ function? In other words, what does adding the keyword throw() actually do? I've read that a function declaration such as void do_something() throw(); should guarantee that no exceptions originate from the do_something() function; however, this doesn't seem to hol...

Throwing exceptions from constructors

Im having a debate with a co-worker about throwing exceptions from constructors, and thought I would like some feedback. Is it ok to throw exceptions from constructors, form a design point of view? Lets say I'm wrapping a posix mutex in a class, it would look something like this. class Mutex { public: Mutex() { if(pthread_mutex_...

C#: Do you raise or throw an exception?

I know that this probably doesn't really matter, but I would like to know what is correct. If a piece of code contains some version of throw new SomeKindOfException(). Do we say that this piece of code can potentially raise an exception? Or throw an exception? The keyword is throw, so I am kind of leaning towards that, but to raise an ...

Do I have to break after throwing exception?

I'm writing a custom class in C# and I'm throwing a couple exceptions if people give the wrong inputs in some of the methods. If the exception is thrown, will any of the code in the method after the throw still be executed? Do I have to put a break after the throw, or does a throw always quit the method? ...

Destructor that calls a function that can throw exception in C++

I know that I shouldn't throw exceptions from a destructor. If my destructor calls a function that can throw an exception, is it OK if I catch it in the destructor and don't throw it further? Or can it cause abort anyway and I shouldn't call such functions from a destructor at all? ...

In C++, if throw is an expression, what is its type?

I picked this up in one of my brief forays to reddit: http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/ Basically, the author points out that in C++: throw "error" is an expression. This is actually fairly clearly spelt out in the C++ Standard, both in the main text and the grammar. However, what...

Throwing an Exception Not Defined in the Interface

What is the best practice to follow when you need to throw an exception which was not defined in an interface that you are implementing? Here is an example: public interface Reader { public abstract void read() throws IOException; } public class CarrotReader implements Reader { public void read() throws IOException {} } publi...

C# Real Time Try Catch

I'd like a response from someone who actually does real-time programming in C# or who really understands the language internals. I know that exceptions should not be used to handle normal processing, but only to detect error conditions. There is plenty of discussion on that topic. I'd like to know if there is any run time slow-down fr...

Exception libraries for C (not C++)

Hi - I am rolling my own exception library for C and would like good examples to examine. So far, I have been looking at David Hanson's: http://drhanson.net/work/ But I know I've seen other ones available in the past. Can you send me some additional pointers? Thanks, SetJmp ...

What is the difference between throw and throw with arg of caught exception?

Imagine two similar pieces of code: try { [...] } catch (myErr &err) { err.append("More info added to error..."); throw err; } and try { [...] } catch (myErr &err) { err.append("More info added to error..."); throw; } Are these effectively the same or do they differ in some subtle way? For example, does the first one c...

Throwing errors in Javascript with error object relevancy

This is pretty much IE related because IE is the environment I'm using to test this, but I want to know if you can affect the relevancy of the error object properties when you throw an error. Consider the following javascript: function MyClass (Arg1, Arg2) // Line 5 of my.js { if (typeof Arg1 != "string") throw new Error...

What is the point of `void func() throw(type)` ?

I know this is a valid c++ program. What is the point of the throw in the function declarement? AFAIK it does nothing and isnt used for anything. #include <exception> void func() throw(std::exception) { } int main() { return 0; } ...

Castle Windsor ResolveAll that throws when any of registered services cannot be resolved

It seems that current behaviour of Castle Windsor (2.0) method container.ResolveAll(Type type) is to ignore all services that cannot be resolved due to missing dependencies. What is recommened way to resolve all services + throwing exception when any of services cannot be resolved? ...