raise

Is it OK to raise a built-in exception, but with a different message, in Python?

纵饮孤独 提交于 2019-12-03 23:46:16
Is it OK to raise a built-in exception with a custom text? or to raise a built-in warning also with custom text? The documentation reads: exception ValueError: Raised when a built-in operation or function receives an argument (…) Is it implied that only built-in operations should raise a ValueError exception? In practice, I understand that it is safe to create an exception class that inherits from ValueError or Exception. But is it OK not to do that, and directly raise a ValueError("custom text")? Since ValueError is built-in, raising a ValueError (with a custom text) allows users to quickly

Raise an event in C# [duplicate]

梦想的初衷 提交于 2019-12-03 09:51:25
This question already has an answer here: Checking for null before event dispatching… thread safe? 6 answers I came across this question in a Microsoft Practice Test and I got confused. Here is the question: Which of the following C# code samples is the proper way to raise an event, assuming that the Alarm event, the AlarmEventArgs class, and the AlarmEventHandler delegate have been declared? Here is the "correct" answer they provided: AlarmEventArgs e = new AlarmEventArgs(1, 2); AlarmEventHandler handler = Alarm; if (handler != null) { handler(this, e); } However, there is also another answer

Ruby Kernel.raise method throws error when enclosing parameters in parenthesis

回眸只為那壹抹淺笑 提交于 2019-12-02 06:44:21
I like method parameters enclosed in parenthesis, this is some Pascal nostalgia. When cleaning up code, if I find a method parameters without it I enclose them immediately. Today it caused my working code throwing errors although my syntax looks okay according to the documentation. Kernel.raise's documentation has this format: (Object) raise(exception[, string [, array]]) These are all working: > raise TypeError TypeError: TypeError > raise (TypeError) TypeError: TypeError > raise "Error message" RuntimeError: Error message > raise ("Error message") RuntimeError: Error message But the enclosed

Setting an exit code for a custom exception in python

十年热恋 提交于 2019-12-01 03:48:37
I'm using custom exceptions to differ my exceptions from Python's default exceptions. Is there a way to define a custom exit code when I raise the exception? class MyException(Exception): pass def do_something_bad(): raise MyException('This is a custom exception') if __name__ == '__main__': try: do_something_bad() except: print('Oops') # Do some exception handling raise In this code, the main function runs a few functions in a try code. After I catch an exception I want to re-raise it to preserve the traceback stack. The problem is that 'raise' always exits 1. I want to exit the script with a

Exception libraries for C (not C++)

时光毁灭记忆、已成空白 提交于 2019-12-01 02:36:12
问题 I am rolling my own exception library for C and would like good examples to examine. So far, I have been looking at David Hanson's: http://drhanson.net/work/ But I know I've seen other ones available in the past. Can you send me some additional pointers? Thanks, SetJmp 回答1: Here is one, compatible with C89 and implementing the try/catch/finally schema as can be found in other OO languages. 回答2: Symbian implemented exceptions (called 'leaves') in terms of longjmp. This was C++ code, but

Raising exceptions without 'raise' in the traceback? [duplicate]

佐手、 提交于 2019-12-01 02:34:54
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Don't show Python raise-line in the exception stack Built in exceptions like NameError etc. give me a traceback to the point in my code where the exception occurred. I'm working on a utility module and it bugs me that if code using my module raises and exception the last thing in the traceback before the exception is my raise WhateverError . Is there any way to raise an exception in python and have the tracback

Setting an exit code for a custom exception in python

非 Y 不嫁゛ 提交于 2019-12-01 00:53:21
问题 I'm using custom exceptions to differ my exceptions from Python's default exceptions. Is there a way to define a custom exit code when I raise the exception? class MyException(Exception): pass def do_something_bad(): raise MyException('This is a custom exception') if __name__ == '__main__': try: do_something_bad() except: print('Oops') # Do some exception handling raise In this code, the main function runs a few functions in a try code. After I catch an exception I want to re-raise it to

How to use pytest to check that Error is NOT raised

社会主义新天地 提交于 2019-11-30 10:42:29
Let's assume we have smth like that : import py, pytest ERROR1 = ' --- Error : value < 5! ---' ERROR2 = ' --- Error : value > 10! ---' class MyError(Exception): def __init__(self, m): self.m = m def __str__(self): return self.m def foo(i): if i < 5: raise MyError(ERROR1) elif i > 10: raise MyError(ERROR2) return i # ---------------------- TESTS ------------------------- def test_foo1(): with pytest.raises(MyError) as e: foo(3) assert ERROR1 in str(e) def test_foo2(): with pytest.raises(MyError) as e: foo(11) assert ERROR2 in str(e) def test_foo3(): .... foo(7) .... Q: How can I make test_foo3(

Proper way of raising events from C++/CLI?

北慕城南 提交于 2019-11-30 08:28:53
I was wondering what's the proper way of raising events from C++/CLI. In C# one s hould first make a copy of the handler, check if it's not null, and then call it . Is there a similar practice for C++/CLI? C++/CLI allows you to override raise in custom event handlers so you don't have to test for null or copy when raising the event. Of course, inside your custom raise you still have to do this. Example, adapted from the MSDN for correctness: public delegate void f(int); public ref struct E { f ^ _E; public: void handler(int i) { System::Console::WriteLine(i); } E() { _E = nullptr; } event f^

Difference between Raise Try and Assert

你说的曾经没有我的故事 提交于 2019-11-29 23:06:08
I have been learning Python for a while and the raise function and assert are (what I realised is that both of them crash the app, unlike try - except) really similar and I can't see a situation where you would use raise or assert over try . So, what is the difference between Raise, Try and Assert? Assert: Used when you want to "stop" the script based on a certain condition and return something to help debug faster: list_ = ["a","b","x"] assert "x" in list_, "x is not in the list" print("passed") #>> prints passed list_ = ["a","b","c"] assert "x" in list_, "x is not in the list" print("passed"