raise

throw exception to another exception in one def using python

throw exception to another exception in one def using python Question: I’ve function to calculate data and after successful calculation the mail has been send to user. but now I want to do error mapping for user interface to show error to users, so that they understand where exactly the error is getting, In their …

Total answers: 2

How to use pytest to confirm proper exception is raised

How to use pytest to confirm proper exception is raised Question: I have the following code to create an Object account. I raise an error if the account meets certain conditions, e.g. is too long. I want to use pytest to test that that functionality works. class Account: def __init__(self, acct): self.tagged = {} self.untagged …

Total answers: 1

Raise a python exception without "raise" statement

Raise a python exception without "raise" statement Question: Is it possible to raise an exception in Python without using raise statement? For example, instead of raise ValueError use some method like ValueError.raise(). This question only relates to python built-in exceptions and not some custom exception classes that can be build from them. Asked By: Max …

Total answers: 2

What's the difference between raise, try, and assert?

What's the difference between raise, try, and assert? Question: 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 …

Total answers: 10

How to use pytest to check that Error is NOT raised

How to use pytest to check that Error is NOT raised Question: 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 …

Total answers: 4

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

How to re-raise an exception in nested try/except blocks? Question: 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 …

Total answers: 4

How to use "raise" keyword in Python

How to use "raise" keyword in Python Question: 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. Asked By: Capurnicus || Source Answers: It’s used for raising errors. if something: raise Exception(‘My error!’) Some examples here Answered …

Total answers: 6

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

TypeError:exceptions must be old-style classes or derived from BaseException, not str Question: 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 …

Total answers: 3

raise statement on a conditional expression

raise statement on a conditional expression Question: How do I elegantly implement the "Samurai principle" (return victorious, or not at all) on my functions? return <value> if <bool> else raise <exception> Asked By: F.D.F. || Source Answers: Well, you could test for the bool separately: if expr: raise exception(‘foo’) return val That way, you could …

Total answers: 5

Best practice for using assert?

Best practice for using assert? Question: Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes? Is assert x >= 0, ‘x is less than zero’ better or worse than if x < 0: raise Exception(‘x is less than zero’) …

Total answers: 15