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
14
votes
4 answers

Convincing Swift that a function will never return, due to a thrown Exception

Because Swift does not have abstract methods, I am creating a method whose default implementation unconditionally raises an error. This forces any subclass to override the abstract method. My code looks like this: class SuperClass { func…
exists-forall
  • 4,148
  • 4
  • 22
  • 29
13
votes
3 answers

A standard way in C++ to define an exception class and to throw exceptions

I want to build a class with functions that may throw exceptions that I want to catch when I use it. I inherit my_exception from the standard exception class. I implement the what() function so that it returns a string that is stored in a private…
ip84
  • 190
  • 1
  • 1
  • 9
13
votes
12 answers

How to throw good exceptions?

I heard you should never throw a string because there is a lack of information and you'll catch exceptions you dont expect to catch. What are good practice for throwing exceptions? do you inherit a base exception class? Do you have many exceptions…
user34537
13
votes
1 answer

Throwing an rvalue

Consider the fragment: try { Foo f; throw std::move(f); } catch (Foo& f) { } [expr.throw] says that: the type of the exception object is determined by removing any top-level cv-qualifiers from the static type of the operand and adjusting…
Barry
  • 286,269
  • 29
  • 621
  • 977
13
votes
5 answers

Can I overload the throw keyword?

I want to overload the throw keyword to catch a class which inherits from Exception and to have it do some logging and other stuff before the actual throw. Is this possible? Or do I have to use a regular function? I've tried: public class…
Amit Lipman
  • 687
  • 7
  • 19
12
votes
4 answers

C++: Throwing a derived class by reference does not work when catching base class

I want to throw my own exceptions with the base class Exception. There is a virtual method print which will be overwritten by the subclasses. I only catch the type Exception& and use print to get the specific error. The problem is that once I throw…
user1286875
  • 193
  • 1
  • 6
12
votes
2 answers

NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class

I have one app with multiple targets (each target is for another client as separate application with different name, bundle identifier etc). I have method: fileprivate static func loadSessionFromKeychain() -> UserSession? { if let sessionData =…
David
  • 825
  • 10
  • 33
12
votes
2 answers

How to throw an error in MySql procedure?

What is the mechanism to force the MySQL to throw an error within the stored procedure? I have a procedure which call s another function: PREPARE my_cmd FROM @jobcommand; EXECUTE my_cmd; DEALLOCATE PREPARE my_cmd; the job command…
Arman
  • 4,566
  • 10
  • 45
  • 66
12
votes
1 answer

Custom error message of re-thrown exception not printed by what()

I'm writing a set of custom exceptions that extend std::exception. In some code, when an exception is caught, I just re-throw it up the chain until the driver main function call catches and prints the result. However, ultimately all that gets…
marcman
  • 3,233
  • 4
  • 36
  • 71
12
votes
1 answer

What is the difference between Exception, InvalidArgumentException or UnexpectedValueException?

When should I use Exception, InvalidArgumentException or UnexpectedValueException? I don't know the real different between them as I always used Exception.
Junior
  • 11,602
  • 27
  • 106
  • 212
12
votes
2 answers

difference between throw and throw ex in c# .net

Can anyone tell me difference between throw and throw ex in brief? I read that throw stores previous exceptions, not getting this line. Can i get this in brief with example?
maverickabhi
  • 197
  • 1
  • 6
  • 21
11
votes
2 answers

why does it cause termination if I try to throw something inside a catch block in C++

I have the following C++ code and it gives me a surprise. The problem is that if I throw something except re-throw inside the catch block, the program will be terminated by calling abort and give the error message in GCC4, "terminate called after…
CS Pei
  • 10,869
  • 1
  • 27
  • 46
11
votes
1 answer

Dart What is the difference between throw and rethrow?

It might be obvious, but I still fail to understand the difference between throw and rethrow and when do either of those should be used?
user13848261
11
votes
4 answers

What is the benefit to limiting throws allowed by a C++ function?

What is the benefit of declaring the possible exception-throws from a C++ function? In other words, what does adding the keyword throw() actually do? I've read that a function declaration such as void do_something() throw(); should guarantee that no…
Rob
  • 141
  • 2
  • 7
11
votes
2 answers

swift lazy var with throw init behavior

I am not sure if it is a bug or it is really how things should work? class A { init() throws { } } class B { lazy var instance = A() } this code compiles without mistakes using XCode 9 and latest Swift version, and works perfect unless…
JuicyFruit
  • 2,638
  • 2
  • 18
  • 35