throw

What is the difference between `throw new Error` and `throw someObject`?

假如想象 提交于 2019-11-26 07:52:18
问题 I want to write a common error handler which will catch custom errors thrown on purpose at any instance of the code. When I did throw new Error(\'sample\') like in the following code try { throw new Error({\'hehe\':\'haha\'}); // throw new Error(\'hehe\'); } catch(e) { alert(e); console.log(e); } Log shows in Firefox as Error: [object Object] and I couldn’t parse the object. For the second throw the log shows as: Error: hehe Whereas when I did try { throw ({\'hehe\':\'haha\'}); } catch(e) {

Difference between C++03 throw() specifier C++11 noexcept

人盡茶涼 提交于 2019-11-26 06:15:38
问题 Is there any other difference between throw() and noexcept apart from being checked runtime and compile time respectively ? Wikipedia C++11 article suggests that C++03 throw specifiers are deprecated. Why so, is noexcept capable enough to cover all that at compile time ? [Note: I referred this question and this article, but couldn\'t got the solid reason of deprecation.] 回答1: Exception specifiers were deprecated because exception specifiers are generally a terrible idea. noexcept was added

Why can I not throw inside a Promise.catch handler?

二次信任 提交于 2019-11-26 04:34:34
问题 Why can\'t I just throw an Error inside the catch callback and let the process handle the error as if it were in any other scope? If I don\'t do console.log(err) nothing gets printed out and I know nothing about what happened. The process just ends... Example: function do1() { return new Promise(function(resolve, reject) { throw new Error(\'do1\'); setTimeout(resolve, 1000) }); } function do2() { return new Promise(function(resolve, reject) { setTimeout(function() { reject(new Error(\'do2\'))

Throwing exceptions from constructors

末鹿安然 提交于 2019-11-26 04:04:16
问题 I\'m 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, from 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_init(&mutex_, 0) != 0) { throw MutexInitException(); } } ~Mutex() { pthread_mutex_destroy(&mutex_); } void lock() { if (pthread_mutex_lock(&mutex_) != 0) { throw

Should I use an exception specifier in C++?

不想你离开。 提交于 2019-11-26 02:42:15
问题 In C++, you can specify that a function may or may not throw an exception by using an exception specifier. For example: void foo() throw(); // guaranteed not to throw an exception void bar() throw(int); // may throw an exception of type int void baz() throw(...); // may throw an exception of some unspecified type I\'m doubtful about actually using them because of the following: The compiler doesn\'t really enforce exception specifiers in any rigorous way, so the benefits are not great.