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
60
votes
19 answers

Is it okay to throw NullPointerException programmatically?

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) …
stratwine
  • 3,663
  • 2
  • 26
  • 32
57
votes
6 answers

Exception vs Assert?

Possible Duplicate: design by contract tests by assert or by exception? Is there a rule of thumb to follow when deciding to use exceptions instead of asserts (or vice versa). Right now I do only throw if it's something I think will happen during…
user34537
57
votes
5 answers

Using `throw;` on a modified exception

I have a function foo that can throw a bar exception. In another function I call foo but I have the ability to add some more detail to the bar exception if thrown. (I'd rather not pass such information as a parameter to foo as it doesn't really…
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
56
votes
3 answers

What does a single "throw;" statement do?

These days, I have been reading a lot the C++ F.A.Q and especially this page. Reading through the section I discovered a "technique" that the author calls "exception dispatcher" that allows someone to group all his exception handling in one handy…
ereOn
  • 53,676
  • 39
  • 161
  • 238
54
votes
5 answers

catch exception by pointer in C++

I found that there are three ways to catch an exception, what are the differences? 1) catch by value; 2) catch by reference; 3) catch by pointer; I only know that catch by value will invoke two copies of the object, catch by reference will invoke…
skydoor
  • 25,218
  • 52
  • 147
  • 201
50
votes
3 answers

Deprecated throw-list in C++11

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?
ABu
  • 10,423
  • 6
  • 52
  • 103
48
votes
3 answers

rethrowing python exception. Which to catch?

I'm learning to use python. I just came across this article: http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html It describes rethrowing exceptions in python, like this: try: do_something_dangerous() except: …
Bosiwow
  • 2,025
  • 3
  • 28
  • 46
48
votes
4 answers

Difference between Throws in method signature and Throw Statements in Java

I am trying to make it clear of the difference between Throws in method signature and Throw Statements in Java. Throws in method signature is as following: public void aMethod() throws IOException{ FileReader f = new…
Weishi Z
  • 1,629
  • 5
  • 18
  • 38
46
votes
3 answers

Can I declare that a php function throws an exception?

Can I declare a function in php that throws an exception? For example: public function read($b, $off, $len) throws IOException
shay
  • 1,317
  • 4
  • 23
  • 35
45
votes
5 answers

"rxjs" observable.throw is not a function - Angular4

I've been learning Angular 4 and everything was going smoothly until I tried to implement catch handling in a service. I'm trying to use "rxjs" catch and throw but I've got an undefined function error in my console. import { Injectable } from…
nick gowdy
  • 6,191
  • 25
  • 88
  • 157
43
votes
6 answers

What is the difference between throw e and throw new Exception(e)?

Consider: try { // Some code here } catch (IOException e) { throw e; } catch (Exception e) { throw e; } What is the difference between throw e and throw new Exception(e)? try { // Some code here } catch (IOException e) { throw…
Vinaya Nayak
  • 1,083
  • 3
  • 16
  • 29
43
votes
12 answers

Incorrect stacktrace by rethrow

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…
Floyd
  • 1,898
  • 12
  • 20
42
votes
2 answers

generic throw giving Expected an object to be thrown lint error

Below throw code giving lint error Expected an object to be thrown no-throw-literal throw { code : 403, message : myMessage }; if i try throw new Error, i am not getting eslint but it gives [Object Object] in the response. throw new Error({ code :…
Munna Babu
  • 5,496
  • 6
  • 29
  • 44
39
votes
3 answers

SQL only a throw inside if statement

I am adding some validation to a couple of stored procedures and need to check if some of the variables are not null (they are populated earlier in the stored procedure). I have been trying to add a "throw" inside an if statement like below: IF…
Edmund G
  • 545
  • 1
  • 5
  • 9
37
votes
8 answers

Should I throw exceptions in an if-else block?

Here is the code: public Response getABC(Request request) throws Exception { Response res = new Response(); try { if (request.someProperty == 1) { // business logic } else { throw new…
Lauda Wang
  • 869
  • 3
  • 11
  • 19
1
2
3
59 60