raise

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

本小妞迷上赌 提交于 2019-11-29 11:44:57
问题 I was wondering what's the proper way of raising events from C++/CLI. In C# one should 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? 回答1: 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

raise statement on a conditional expression

心已入冬 提交于 2019-11-29 10:59:50
问题 Following "Samurai principle", I'm trying to do this on my functions but seems it's wrong... return <value> if <bool> else raise <exception> Is there any other "beautiful" way to do this? Thanks 回答1: Inline/ternary if is an expression, not a statement. Your attempt means "if bool, return value, else return the result of raise expression " - which is nonsense of course, because raise exception is itself a statement not an expression. There's no way to do this inline, and you shouldn't want to.

Raising WPF MouseLeftButtonDownEvent event

二次信任 提交于 2019-11-29 06:06:10
I am trying ot raise a MouseLeftButtonDownEvent by bubbling it up the Visual tree with the following code. MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice,0, MouseButton.Left); args.RoutedEvent = UIElement.MouseLeftButtonDownEvent; args.Source = this; RaiseEvent(args); For some reason the higher level components are not receiving this bubbled event. Am I overlooking something or is it not possible to raise this Mouse event Your problem is that you are raising an event that does not bubble. MouseLeftButtonDownEvent is defined as RoutingStrategy.Direct , which means it

Rails ActiveSupport: How to assert that an error is raised?

强颜欢笑 提交于 2019-11-29 00:53:19
I am wanting to test a function on one of my models that throws specific errors. The function looks something like this: def merge(release_to_delete) raise "Can't merge a release with itself!" if( self.id == release_to_delete.id ) raise "Can only merge releases by the same artist" if( self.artist != release_to_delete.artist ) #actual merge code here end Now I want to do an assert that when I call this function with a parameter that causes each of those exceptions, that the exceptions actually get thrown. I was looking at ActiveSupport documentation, but I wasn't finding anything promising. Any

TypeError:exceptions must be old-style classes or derived from BaseException, not str

孤街浪徒 提交于 2019-11-28 04:51:37
Following is my code: test = 'abc' if True: raise test + 'def' And when i run this, it gives me the TypeError TypeError: exceptions must be old-style classes or derived from BaseException, not str So what kind of type should the test be? The sole argument to raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from Exception). Try this: test = 'abc' if True: raise Exception(test + 'def') You can't raise a str . Only Exception s can be raise d. So, you're better off constructing an exception with that string and

Raising WPF MouseLeftButtonDownEvent event

↘锁芯ラ 提交于 2019-11-27 23:35:05
问题 I am trying ot raise a MouseLeftButtonDownEvent by bubbling it up the Visual tree with the following code. MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice,0, MouseButton.Left); args.RoutedEvent = UIElement.MouseLeftButtonDownEvent; args.Source = this; RaiseEvent(args); For some reason the higher level components are not receiving this bubbled event. Am I overlooking something or is it not possible to raise this Mouse event 回答1: Your problem is that you are raising an

How to re-raise an exception in nested try/except blocks?

折月煮酒 提交于 2019-11-27 11:59:53
I know that if I want to re-raise an exception, I simple use raise without arguments in the respective except block. But given a nested expression like try: something() except SomeError as e: try: plan_B() except AlsoFailsError: raise e # I'd like to raise the SomeError as if plan_B() # didn't raise the AlsoFailsError how can I re-raise the SomeError without breaking the stack trace? raise alone would in this case re-raise the more recent AlsoFailsError . Or how could I refactor my code to avoid this issue? You can store the exception type, value, and traceback in local variables and use the

Don't show Python raise-line in the exception stack

这一生的挚爱 提交于 2019-11-27 07:51:15
When I raise my owns exceptions in my Python libraries, the exception stack shows the raise-line itself as the last item of the stack. This is obviously not an error, is conceptually right, but points the focus on something that is not useful for debugging when you're are using code externally, for example as a module. Is there a way to avoid this and force Python to show the previous-to-last stack item as the last one, like the standard Python libraries. Due warning: modifying the behaviour of the interpreter is generally frowned upon. And in any case, seeing exactly where an error was raised

TypeError:exceptions must be old-style classes or derived from BaseException, not str

血红的双手。 提交于 2019-11-27 05:24:19
问题 Following is my code: test = 'abc' if True: raise test + 'def' And when i run this, it gives me the TypeError TypeError: exceptions must be old-style classes or derived from BaseException, not str So what kind of type should the test be? 回答1: The sole argument to raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from Exception). Try this: test = 'abc' if True: raise Exception(test + 'def') 回答2: You can't raise a

How to use “raise” keyword in Python [duplicate]

耗尽温柔 提交于 2019-11-27 02:36:09
This question already has an answer here: Manually raising (throwing) an exception in Python 7 answers I have read the official definition of "raise", but I still don't quite understand what it does. In simplest terms, what is "raise"? Example usage would help. Ignacio Vazquez-Abrams It has 2 purposes. yentup has given the first one. It's used for raising your own errors. if something: raise Exception('My error!') The second is to reraise the current exception in an exception handler, so that it can be handled further up the call stack. try: generate_exception() except SomeException as e: if