Questions tagged [throw]

throw is a keyword in various languages used for signaling an exception.

By throwing an exception the normal flow of control of a program gets interrupted. If this happens inside a try-block execution continues inside a matching catch or finally block, like in the following Java example:

try { 
    throw new RuntimeException("for demonstrating try-throw-catch");
    System.out.println("this doesn't get executed")
} catch (RuntimeException re) {
    System.out.println("flow of control goes directly here");
}

Use this tag for questions about the process or syntax of throwing exceptions.

Prefer for questions about the declaration of exceptions thrown by a method

Use or for questions about the catching side of the exception handling.

and for questions about the complete process of exception handling.

886 questions
611
votes
13 answers

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

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…
Jayapal Chandran
  • 10,600
  • 14
  • 66
  • 91
341
votes
11 answers

Throwing exceptions from constructors

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…
lkristjansen
  • 3,514
  • 3
  • 18
  • 7
154
votes
7 answers

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

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…
demian85
  • 2,394
  • 3
  • 20
  • 20
135
votes
14 answers

Should I use an exception specifier in C++?

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…
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
131
votes
7 answers

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

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 scenes, so I can make decisions of when to use them and whether they are slow. From what I…
user34537
117
votes
4 answers

In C++, if throw is an expression, what is its type?

I picked this up in one of my brief forays to reddit: http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/ Basically, the author points out that in C++: throw "error" is an expression. This is actually fairly…
anon
113
votes
3 answers

What is the difference between C++03 `throw()` specifier and C++11 `noexcept`?

Is there any difference between throw() and noexcept other than being checked at runtime and compile time respectively? This Wikipedia C++11 article suggests that the C++03 throw specifiers are deprecated. Why so ... Is the noexcept capable enough…
iammilind
  • 68,093
  • 33
  • 169
  • 336
95
votes
3 answers

Is there a throws keyword in C# like in Java?

Possible Duplicate: how to use Java-style throws keyword in C#? i have a function where an exception occurs say for example private void functionName() throws Exception { // some code that might throw an exception } thanks!
user677607
87
votes
7 answers

T-SQL Throw Exception

I am facing the famous 'Incorrect syntax' while using a THROW statement in a T-SQL stored procedure. I have Googled it and checked the questions on StackOverflow but the solutions proposed (and strangely, accepted) do not work for me. I am modifying…
user3021830
  • 2,784
  • 2
  • 22
  • 43
83
votes
8 answers

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

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…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
69
votes
5 answers

What is generator.throw() good for?

PEP 342 (Coroutines via Enhanced Generators) added a throw() method to generator objects, which allows the caller to raise an exception inside the generator (as if it was thrown by the yield expression). I am wondering what the use cases for this…
NikiC
  • 100,734
  • 37
  • 191
  • 225
66
votes
6 answers

"throw new Warning" in JavaScript?

At the moment I'm extending my JavaScript project with error handling. The throw statement is playing an important role here: throw new Error("text"); // Error: text However, can I also throw a warning? I tried the following to no avail: throw new…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
62
votes
3 answers

What can you throw in Java?

Conventional wisdom says you can only throw objects that extend Throwable in Java, but is it possible to disable the bytecode verifier and get Java to compile and run code that throws arbitrary objects - or even primitives? I looked up the JVM's…
NullUserException
  • 83,810
  • 28
  • 209
  • 234
60
votes
2 answers

Why is 'throws' not type safe in Swift?

The biggest misunderstanding for me in Swift is the throws keyword. Consider the following piece of code: func myUsefulFunction() throws We cannot really understand what kind of error it will throw. The only thing we know is that it might throw…
Noobass
  • 1,974
  • 24
  • 26
60
votes
8 answers

Exception handling : throw, throws and Throwable

Can any of you explain what the differences are between throw, throws and Throwable and when to use which?
Sumithra
  • 6,587
  • 19
  • 51
  • 50
1
2 3
59 60