try catch finally - when doc->StartUndo()
-
On 07/08/2013 at 04:11, xxxxxxxx wrote:
While I agree, if you are wanting to handle STL exceptions (and errors) you may want to brace code that has those possibilities - such as mathematical algorithms and such beyond the normal STL exceptions. Otherwise, I have never seen any problems in bracing larger sections of code. There might be a very small speed hit at most.
-
On 07/08/2013 at 04:27, xxxxxxxx wrote:
Originally posted by xxxxxxxx
It would make sense to have the try/catch on everything when exception handling is present.
Really ?
I don't think this is a good idea.I agree with Robert here. It is a very good idea in C#, so why not in C++?
"Things" will invariably happen, especially when users are involved, - and they usually are.-Ingvar
-
On 07/08/2013 at 04:46, xxxxxxxx wrote:
Yeah, i actually did that in order to deal with failed memory allocations and IO errors in my current project. Basically try/catch around some complicated computations. If e.g. std::bad_alloc is caught it would just unwind to the top level function and return INITRENDER_OUT_OF_MEMORY, without me having to clutter my code with checks for every little allocation
Don't forget to enable exception handling in the compiler options though. It is disabled by default.
Also, none of the C4D API functions throws exceptions, so you have to check for errors manually or write wrappers which throw exceptions.
For something like the Undo, you could also use a pattern where some arbitrary code is executed on scope exit but without having to write a special class for RAII. For example http://code.google.com/p/scope-exit/
or http://www.boost.org/doc/libs/1_54_0/libs/scope_exit/doc/html/index.html I never tried it but i figured it should work nicely with c++11 lambdas, etc. -
On 07/08/2013 at 06:13, xxxxxxxx wrote:
I think that both Visual Studio and Xcode will error during compilation if there is exception syntax and exception handling isn't enabled. But good to note that it needs to be enabled nonetheless.
-
On 07/08/2013 at 06:25, xxxxxxxx wrote:
I have exception syntax and do not get an error. And I have never turned exception handling on, in fact, I have no idea how it is done in Visual Studio. I take it for granted it is on..
-
On 07/08/2013 at 07:37, xxxxxxxx wrote:
Sorry, i meant it is disabled in the cinema4dsdk project. In VC it is just a warning if exception syntax is used while compiler support is disabled.
The setting is in the project properties page Configuration Properties/C/C++/Code Generation/Enable C++ Exceptions. -
On 08/08/2013 at 00:02, xxxxxxxx wrote:
Originally posted by xxxxxxxx
I agree with Robert here. It is a very good idea in C#, so why not in C++?
Some food for thought why you should avoid exceptions whenever possible (and that 's just the beginning if you dig into it) :
http://c2.com/cgi/wiki?AvoidExceptionsWheneverPossible
http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl
http://programmers.stackexchange.com/questions/107723/arguments-for-or-against-using-try-catch-as-logical-operators
http://stackoverflow.com/questions/1744070/why-should-exceptions-be-used-conservativelyBest regards,
Wilfried
-
On 08/08/2013 at 00:24, xxxxxxxx wrote:
Right, but if you use anything (a third-party lib or the STL) that requires exception handling, then you MUST handle them or suffer the consequences (Cinema 4D crashes). Not much choice there.
-
On 08/08/2013 at 01:14, xxxxxxxx wrote:
Originally posted by xxxxxxxx
Right, but if you use anything (a third-party lib or the STL) that requires exception handling, then you MUST handle them or suffer the consequences (Cinema 4D crashes). Not much choice there.
Maybe a requirement for 3rd party libs but not necessarily for STL. For Win I know that you can turn it off (you've to use the #define _HAS_EXCEPTIONS 0 #define _STATIC_CPPLIB besides switching off exceptions) and for OS X I'm pretty sure you can too, as the LLVM compiler project (while using STL) has the rule of not using RTTI or exceptions (see: http://llvm.org/docs/CodingStandards.html#do-not-use-rtti-or-exceptions) :
"These two language features violate the general C++ principle of "you only pay for what you use", causing executable bloat even if exceptions are never used in the code base, or if RTTI is never used for a class. Because of this, we turn them off globally in the code."
Best regards,
Wilfried
-
On 08/08/2013 at 02:06, xxxxxxxx wrote:
Never knew about that (but then I don't use exception handling very often). How is error handling done then? Does this switch to an alternative codebase that returns errors instead?
-
On 08/08/2013 at 03:35, xxxxxxxx wrote:
Yes scope-exit can be very helpful, but only if you use C++11.
Here is how I would do it using my SCOPE_EXIT implementation.
doc->StartUndo(); SCOPE_EXIT { doc->EndUndo(); } ...
-
On 08/08/2013 at 03:39, xxxxxxxx wrote:
Ok, I am not using exceptions as type checking or value approval. I use try-catch in case something goes wrong which I had not thought of beforehand.
Some very good programmers will foresee any possible event and any bad function argument and any peculiar user behaviour and write the appropriate code to tackle this.
While I am not such a good programmer, I instead type check and validate values as best as I can, then I want to wrap it up in try-catch for final safety. I just do not want my plugins to crash C4D.In C#, which is my job (C4D plugins is a hobby), everything I write is wrapped in try-catch, sometimes also -finally. Mostly I benefit from this during development and debugging, because if reflection in C#, I immediately know what went wrong, and at what line in what class in what file it happened. The gioal is of course to have as few exceptions as possible in the final product.
And I have very seldom used exceptions to steer the program flow.