throw

Difference between throw and throws in Java? [duplicate]

ⅰ亾dé卋堺 提交于 2019-11-26 17:45:18
问题 This question already has answers here : Exception handling : throw, throws and Throwable (8 answers) Closed 5 years ago . Can any one clearly state the difference between throw and throws in Java exception handling with an example? I have tried googling but couldn't arrive at a conclusion. Pls help 回答1: throws clause is used to declare an exception and throw keyword is used to throw an exception explicitly. If we see syntax wise then throw is followed by an instance variable and throws is

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

不想你离开。 提交于 2019-11-26 15:41:51
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')); }, 1000) }); } do1().then(do2).catch(function(err) { //console.log(err.stack); // This is the only way to

Exception handling : throw, throws and Throwable

◇◆丶佛笑我妖孽 提交于 2019-11-26 15:12:55
Can any of you explain what the differences are between throw , throws and Throwable and when to use which? throws : Used when writing methods, to declare that the method in question throws the specified (checked) exception. As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare throws NullPointerException . throw : Instruction to actually throw the exception. (Or more specifically, the Throwable ). The throw keyword is followed by a reference to a Throwable (usually an exception). Example: Throwable : A class which you

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

做~自己de王妃 提交于 2019-11-26 14:51:21
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 stop doing the return. How does it check when to do stop? I am taking a guess and saying there is a second stack which holds the type of exception and stack location then does returns until it gets there. I am also guessing the only time that stack is

Throwing exceptions from constructors

蓝咒 提交于 2019-11-26 14:49:01
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 MutexLockException(); } } void unlock() { if (pthread_mutex_unlock(&mutex_) != 0) { throw

Deprecated throw-list in C++11

孤街醉人 提交于 2019-11-26 13:01:31
问题 Just as I can see in cppreference, the classic \"throw\" declaration lists is now deprecated in C++11. What is the reason of leaving this mechanism and how should I have to specify what exceptions throws a function of mine? 回答1: For more detailed reasoning, see: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3051.html As expressed in the national body comment above, exception specifications have not proven useful in practice. There are numerous discussions of the problems with

Is it okay to throw NullPointerException programmatically?

我的未来我决定 提交于 2019-11-26 12:29:09
问题 When there is a post-condition, that return value of a method must not be null, what can be done? I could do assert returnValue != null : \"Not acceptable null value\"; but assertions could be turned off! So is it okay to do if(returnValue==null) { throw new NullPointerException(\"return value is null at method AAA\"); } ? Or is it better to use a user-defined exception (like NullReturnValueException ) for such a condition? 回答1: I see no problem with throwing a NPE as early as possible before

When to catch the Exception vs When to throw the Exceptions?

断了今生、忘了曾经 提交于 2019-11-26 12:21:54
问题 I have been coding in Java for a while now. But sometimes, I don\'t understand when I should throw the exception and when should I catch the exception. I am working on a project in which there are lot of methods. The hierarchy is something like this- Method A will call Method B and Method B will call some Method C and Method C will call Method D and Method E. So currently what I am doing is- I am throwing exceptions in all the methods and catching it in Method A and then logging as an error.

Should I use an exception specifier in C++?

落爺英雄遲暮 提交于 2019-11-26 12:02:45
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. Ideally, you would like to get a compile error. If a function violates an exception specifier, I think the

Incorrect stacktrace by rethrow

北慕城南 提交于 2019-11-26 09:26:58
问题 I rethrow an exception with \"throw;\", but the stacktrace is incorrect: static void Main(string[] args) { try { try { throw new Exception(\"Test\"); //Line 12 } catch (Exception ex) { throw; //Line 15 } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex.ToString()); } Console.ReadKey(); } The right stacktrace should be: System.Exception: Test at ConsoleApplication1.Program.Main(String[] args) in Program.cs:Line 12 But I get: System.Exception: Test at ConsoleApplication1.Program.Main