araq / blog Goto Github PK
View Code? Open in Web Editor NEWMy blog posts. You are NOT allowed to distribute these articles without my explicit permission.
My blog posts. You are NOT allowed to distribute these articles without my explicit permission.
A C++ destructor should not raise an exception as it can be called in an exception handler and then it's not clear what to do, see https://isocpp.org/wiki/faq/exceptions#dtors-shouldnt-throw for more details
this is not accurate AFAIK, there are well defined cases where it's legal to do so since c++11, with noexcept(false)
see spec here https://en.cppreference.com/w/cpp/language/destructor
and for more on this, see https://akrzemi1.wordpress.com/2011/09/21/destructors-that-throw/ ; there are valid use cases.
Furthermore setjmp is an expensive call because it has to store a stack snapshot and this cost is always paid, whether an exception was raised or not.
but on the flip side, what's not mentioned here is that the go-style error handling incurs an extra if
branch evaluation for every function in call stack between the raising function and the catching function, which is not the case for setjmp
; so which one is more costly depends on factors such as how deeply nested are exceptions, how frequently they throw, and size of snapshot
Other solutions are conceivable too, including a novel static analyis that detects a rule like "the result of procs that can raise must not be written to a heap location"
that's not sufficient, it should also check the proc has no side effect (eg (recursively) calling printf or other C function); and even that's not sufficient, eg with re-entrant code; eg consider this code:
proc ensure(a: bool) = if not a: raise newException("err")
proc factorial(n: int) : int=
ensure(a>=0)
if n == 0: 1
else: fact(n-1) * n
try: factorial(readLine().toInt)
except: echo "invalid input"
and call it with echo -1 | ./main
=> will give stack overflow with quirky exceptions, even though there aren't side effects (beside setting currentException
which I'm assuming you're not treating as side-effect, otherwise any proc that can raise will have by definition a side-effect, making this distinction useless)
In more complex situations it maybe be much harder to detect. I don't even know if it's even tractable in general.
Try the araq-quirky-exceptions branch of Nim, compile your code with --define:nimQuirky and try it for yourself.
overall I'd be quite nervous with a codebase using quirky exceptions. Unless static analysis can guarantee 100% no change in semantics by skipping the implicit returnOnError
by identifying any potential side effect (or stack overflow, see above example), I'd need to audit entire codebase to make sure each function call is safe to continue on error.
I'd be curious about the more traditional (go-style except it'd be implicit) approach code transformation that handles exceptions by inserting if (unlikely(currentException != nullptr)) goto catchBlock;
after every proc that can raise, and how it compares performance wise to setjmp
approach; at least (in theory) there should be no semantic difference, unlike the quirky exceptions approach.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.