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

Implementation of finally in C++

Is this a good way to implement a Finally-like behavior in standard C++? (Without special pointers) class Exception : public Exception { public: virtual bool isException() { return true; } }; class NoException : public Exception { public:…
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
19
votes
3 answers

Why is throwing a checked exception type allowed in this case?

I noticed by accident that this throw statement (extracted from some more complex code) compiles: void foo() { try { } catch (Throwable t) { throw t; } } For a brief but happy moment I thought that checked exceptions had…
Boann
  • 48,794
  • 16
  • 117
  • 146
19
votes
8 answers

When should I use a ThrowHelper method instead of throwing directly?

When is it appropriate to use a ThrowHelper method instead of throwing directly? void MyMethod() { ... //throw new ArgumentNullException("paramName"); ThrowArgumentNullException("paramName"); ... } void…
smv
  • 585
  • 5
  • 12
18
votes
1 answer

Is there a way in Dart to mark a function as throwing an exception?

I was trying to find a way in Flutter/Dart to mark a function that may throw an exception during its execution. After some time searching in the documentation and Google I did not find any way of doing this. In other language, for example Swift,…
Gabi D
  • 199
  • 1
  • 8
18
votes
3 answers

How to throw InvalidArgumentException JavaScript?

Objective Throw InvalidArgumentException in a JavaScript method like one does in Java or similar languages. Background I have been trying to familiarize myself with JavaSctipt error handling, and I know I can throw exceptions using the throw…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
17
votes
2 answers

Java lambda only throwing expression-style instead of statement-style

In Java (using Java 8 currently), I can write this and all will compile nice and well: Supplier asd = () -> { throw new RuntimeException(); }; Yet, I cannot write this: Supplier asd = () -> throw new RuntimeException(); // This…
Tamir Nauman
  • 279
  • 1
  • 4
17
votes
3 answers

How do you throw Lua error up?

Is it possible to throw a Lua error from a function to be handled by the script calling the function? For example the following will throw an error at indicated comment local function aSimpleFunction(...) string.format(...) -- Error is indicated…
Puddler
  • 2,619
  • 2
  • 17
  • 26
17
votes
3 answers

Difference between throw and throws in Java?

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
user3527594
  • 181
  • 1
  • 1
  • 5
17
votes
3 answers

Re-throw an exception inside catch block

Can anyone please confirm me if this information is correct or not: In C++, inside catch block we can re-throw an exception using throw statement, but the thrown exception should have the same type as the current caught one.
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
16
votes
6 answers

Throwing an Exception Not Defined in the Interface

What is the best practice to follow when you need to throw an exception which was not defined in an interface that you are implementing? Here is an example: public interface Reader { public abstract void read() throws IOException; } public…
Scott
  • 257
  • 2
  • 3
  • 8
15
votes
2 answers

C++ re-throw an exception caught by

how can I re-throw an exception caught by catch(...) block?
MBZ
  • 26,084
  • 47
  • 114
  • 191
14
votes
5 answers

C#: Do you raise or throw an exception?

I know that this probably doesn't really matter, but I would like to know what is correct. If a piece of code contains some version of throw new SomeKindOfException(). Do we say that this piece of code can potentially raise an exception? Or throw an…
Svish
  • 152,914
  • 173
  • 462
  • 620
14
votes
3 answers

Better to return None or throw an exception when fetching URL?

I have a Scala helper method that currently tries to fetch a URL and return an Option[String] with the HTML of that webpage. If there are any exceptions (malformed url, read timeouts, etc...) or if there are any problems it returns a None. The…
James
  • 15,085
  • 25
  • 83
  • 120
14
votes
1 answer

How to replace dynamic exception specifications: throw(...)

I am working on a project that uses a legacy library which uses function definitions like void func() throw(some_exception); Since dynamic exception specifications are removed in C++17 I am wondering how to address this problem. P0003R0 suggests…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
14
votes
3 answers

Throwing an array local to a try block

From C++ Primer 18.1.1: If the [thrown] expression has an array or function type, the expression is converted to its corresponding pointer type. How is it that this program can produce correct output of 9876543210 (g++ 5.2.0)? #include…
SergeantPenguin
  • 843
  • 7
  • 16