throw

Is there a throws keyword in C# like in Java? [duplicate]

感情迁移 提交于 2019-12-03 00:52:31
This question already has answers here : How to use Java-style throws keyword in C#? (8 answers) 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! No, because there are no checked exceptions in C# If you are trying to document exceptions that are thrown, use the standard xml documentation /// <exception cref="InvalidOperationException">Why it's thrown.</exception> No. There is no such construct in c#. But you can add the

What is generator.throw() good for?

只愿长相守 提交于 2019-12-02 16:57:46
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 feature are. Let's say I use a generator to handle adding information to a database; I use this to store network-received information, and by using a generator I can do this efficiently whenever I actually receive data, and do other things otherwise. So, my generator first opens a database connection, and every time you send it something, it'll add a

Exception vs Assert? [duplicate]

℡╲_俬逩灬. 提交于 2019-12-02 15:29:17
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 its something I think will happen during runtime on the user side (like a socket or file error). Almost everything else I use asserts. Also, if I were to throw an assert, what is a nice standard object to throw? IIRC there is std::logic_error but that is not a good object to throw? what would I throw for a missing file or unexpected input (such as from the command line instead of a frontend

Difference between Throws in method signature and Throw Statements in Java

元气小坏坏 提交于 2019-12-02 14:18:15
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 FileReader("notExist.txt"); } Throw Statements is as following: public void bMethod() { throw new IOException(); } From my understanding, a throws in method signature is a notification that the method may throw such an exception. throw statement is what actually throw a created object under according circumstances. In that sense, throws in method signature should always appear if

Why throw at derived class catches by base?

不想你离开。 提交于 2019-12-02 12:51:26
问题 For the code below, result is "EA Exception Finished", which means although we threw at derived class it caught by base class. Is it always? And if so, how can I make the derived class catches, thus "EB Exception Finished" appears? Also I can't exactly get what does it mean by throw EB() and catch(EA&) . And does catch(EA&) means the catch block gets a reference for EA object ? Sorry for my ignorance. If you recommend me a book or something to refer about exception structure, that'd be great

Why, when I am testing that a method throws an exception and the method throw an exception, does the test stop?

自作多情 提交于 2019-12-02 04:13:53
I have a unit test that tests if method throws an exception when condition is present, and method does throws exception as expected. - (void)testMethodThrowsWhenConditionIsPresent { XCTAssertThrows([Foo methodWithCondition: condition], @"Condition is true, method should throw exception"); } Here is the exception source: - (void)methodWithCondition:(someType)condition { if (condition) { [NSException raise: @"condition is true!" format: @"condition is true!"]; } } Why does the test stop at the line the exception is thrown? The test does not go on, it stops at that line, when I expect it to

Why throw at derived class catches by base?

爷,独闯天下 提交于 2019-12-02 04:01:41
For the code below, result is "EA Exception Finished", which means although we threw at derived class it caught by base class. Is it always? And if so, how can I make the derived class catches, thus "EB Exception Finished" appears? Also I can't exactly get what does it mean by throw EB() and catch(EA&) . And does catch(EA&) means the catch block gets a reference for EA object ? Sorry for my ignorance. If you recommend me a book or something to refer about exception structure, that'd be great help. class EA {}; class EB: public EA {}; void F() { throw EB(); // throw at EB(). } int main() { try

JavaScript rethrowing an Exception preserving the stack trace

☆樱花仙子☆ 提交于 2019-12-01 22:18:03
问题 In Chrome, when an exception occurs, it prints a stack trace to the console log. This is extremely useful, but unfortunately in cases where an exception has been rethrown this causes an issue. } catch (e) { if (foo(e)) { // handle the exception } else { // The stack traces points here throw e; } } Unfortunately, the following code in jQuery.js is causing all exceptions to have this issue if they're from inside event handlers. try { while( callbacks[ 0 ] ) { callbacks.shift().apply( context,

JavaScript rethrowing an Exception preserving the stack trace

时间秒杀一切 提交于 2019-12-01 22:01:40
In Chrome, when an exception occurs, it prints a stack trace to the console log. This is extremely useful, but unfortunately in cases where an exception has been rethrown this causes an issue. } catch (e) { if (foo(e)) { // handle the exception } else { // The stack traces points here throw e; } } Unfortunately, the following code in jQuery.js is causing all exceptions to have this issue if they're from inside event handlers. try { while( callbacks[ 0 ] ) { callbacks.shift().apply( context, args ); } } // We have to add a catch block for // IE prior to 8 or else the finally // block will never

Ill-Formed, No Diagnostic Required (NDR): ConstExpr Function Throw in C++14

丶灬走出姿态 提交于 2019-12-01 21:48:15
#include <iostream> using namespace std; constexpr int f(bool b){ return b ? throw 0 : 0; } // OK constexpr int f() { return f(true); } // Ill-Formed, No Diagnostic Required int main(){ try{ f(); }catch( int x ){ cout << "x = " << x << endl; } return 0; } This code is an example from the C++14 Standard (ISO/IEC 14882:2014), Section 7.1.5, Paragraph 5: For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of