开发者

What is the difference between C++, Java and JavaScript exception handling?

They are very different kind of languages and the 开发者_StackOverflow社区way they handle exceptions might be rather different.. How is exception handling implemented and what are the implementation difference within these languages?

I am asking this question also because I noticed that C++ exception handling seems to be very slow compared to the JavaScript version.


The most detailed answer I found regarding Exception handling and performance/implementation is on this page: http://lazarenko.me/tips-and-tricks/c-exception-handling-and-performance


I know just the basics of C++ exception handling but as far as I can see, Java has excplicit Object-based hierarchy for exceptions (Throwable, Exception, RuntimeException, Error) while in C++ you can do

try
{
     throw 1337;
}
catch (int i)
{
    // i == 1337
}

This of course reflects to the design of your class structures and general exception handling policies etc.

Other difference introduced by this seemingly minor difference is that C++ really only has what would be known as Runtime Exceptions in Java world, which means that you can throw anything at any time without explicitly writing code to handle the throw pseudo-exception (I'm not willing to call int or any other primitive type an exception, they're just possibly exceptional values).

Lastly, due to their nature when compared to Java's exceptions C++ exceptions don't by default contain anything comparable to Java's stacktraces.


If you're asking about how it internally generates these exceptions, then it's a pretty complex issue.

One approach (that I think C++ and Java use, I dont know about Javascript), is to maintain a stack of error handlers. When an exception is thrown, the the top entry is popped off the stack and handles the exception appropriately or pops another entry from the stack if it can't handle it (such as it received a NullPointerException when the top handler is a OutOfBoundException).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜