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
37
votes
1 answer

Does throw inside a catch ellipsis (...) rethrow the original error in C++?

If in my code I have the following snippet: try { doSomething(); } catch (...) { doSomethingElse(); throw; } Will the throw rethrow the specific exception caught by the default ellipsis handler?
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
34
votes
3 answers

Why does throwing 2 exceptions in a row not generate an unreachable code warning?

Why do the following lines of code not create a compiler warning? void Main() { throw new Exception(); throw new Exception(); } As I see it, the compiler should inform you that the second throw exception cannot be reached.
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
33
votes
7 answers

How to throw an exception with a status code?

How can throw an error with options or a status code and then catch them? From the syntax here, it seems we can through the error with additional info: new Error(message, options) So, can we throw like this below? throw new Error('New error…
Run
  • 54,938
  • 169
  • 450
  • 748
28
votes
2 answers

C++: Throwing exceptions, use 'new' or not?

Is it proper to use throw new FoobarException(Baz argument); or throw FoobarException(Baz argument);? When catching I always use catch(FoobarException& e) "just in case" but I never could find a solid answer whether I had to use new or not in C++…
Casey
  • 10,297
  • 11
  • 59
  • 88
27
votes
3 answers

continuing execution after an exception is thrown in java

I'm trying to throw an exception (without using a try catch block) and my program finishes right after the exception is thrown. Is there a way that after I throw the exception, to then continue execution of my program? I throw the…
DannyD
  • 2,732
  • 16
  • 51
  • 73
27
votes
9 answers

Using throw in a Javascript expression

Here is what I want to do: var setting = process.env.SETTING || throw new Error("please set the SETTING environmental variable"); ^^^^^ But the interpreter complains about "Syntax Error: Unexpected token…
Andres Riofrio
  • 9,851
  • 7
  • 40
  • 60
27
votes
4 answers

Is it okay to manually throw an std::bad_alloc?

I have this code.. CEngineLayer::CEngineLayer(void) { // Incoming creation of layers. Wrapping all of this in a try/catch block is // not helpful if logging of errors will happen. logger = new (std::nothrow) CLogger(this); …
Jookia
  • 6,544
  • 13
  • 50
  • 60
27
votes
2 answers

How to make a mock object throw an exception in Google Mock?

With Google Mock 1.7.0, I have a mock object with a method, and I want to expect it to be called, and in this case the mocked method should throw an exception. ObjectMock object_mock_; EXPECT_CALL(object_mock_, method()) .Times(1) …
user1735594
  • 437
  • 1
  • 6
  • 9
25
votes
2 answers

I am unable to use THROW SQL Server 2008 R2

SQL Server 2008 R2 Management Studio does not recognized my throw in the below example, it says incorrect syntax near Throw I am trying to throw an error here, so I can handled it in my website when someone insert the same value twice. Begin Try …
user2405574
  • 253
  • 1
  • 3
  • 4
23
votes
6 answers

Do I have to break after throwing exception?

I'm writing a custom class in C# and I'm throwing a couple exceptions if people give the wrong inputs in some of the methods. If the exception is thrown, will any of the code in the method after the throw still be executed? Do I have to put a break…
Ross
  • 1,553
  • 1
  • 14
  • 22
23
votes
2 answers

Can `throw` be inside a comma subexpression within C++ conditional (ternary) operator?

It is well known that throw can be placed as the second or the third operand of C++ ternary operator ?:. But can it be inside a comma subexpression of there operands? It looks like compilers diverge in this regard. Please consider an…
Fedor
  • 17,146
  • 13
  • 40
  • 131
23
votes
6 answers

Is code with try-catch-rethrow equivalent to code w/o try-catch?

Under which circumstances are the following two codes not equivalent? { // some code, may throw and/or have side effects } try { // same code as above } catch(...) { throw; } edit Just to clarify, I'm not interested in (i) deviations from…
Walter
  • 44,150
  • 20
  • 113
  • 196
23
votes
2 answers

What is the difference between throw and throw with arg of caught exception?

Imagine two similar pieces of code: try { [...] } catch (myErr &err) { err.append("More info added to error..."); throw err; } and try { [...] } catch (myErr &err) { err.append("More info added to error..."); throw; } Are these…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
20
votes
2 answers

How do exceptions work (behind the scenes) in C#

Identical to "How do exceptions work (behind the scenes) in C++", but for C#. I know that the steps below have to be performed when an exception is thrown. Find the nearest handler for the exception type; Unwind the stack up to the handler…
jpbochi
  • 4,366
  • 3
  • 34
  • 43
19
votes
1 answer

How to throw an exception and exit the program in Haskell?

I have a question: how do I throw an exception and exit the program? I have writen down a simple example: -- main.hs import Test main = do Test.foo "" putStrLn "make some other things" Here is the module: moldule Test where foo :: String…
develhevel
  • 3,161
  • 4
  • 21
  • 27
1 2
3
59 60