according to python documents, Exception is derived from BaseExceptions and I should use it for user defined exceptions. so I have:
class VisaIOError(Exception):
def __init__(self, error_code):
abbreviation, description = _completion_and_error_messages[error_code]
Error.__init__(self, abbreviation + ": " + description)
self.error_code = error_code
And
raise(visa_exceptions.VisaIOError, status)
but I get (trackback snippet):
File "C:\Python32\Lib\site-packages\pyvisa\vpp43.py", line 400, in check_status
raise(visa_exceptions.VisaIOError, status)
TypeError: exceptions must derive from BaseException
Note: I am converting code from python 27 to 32
The BaseException is the base class of all other exceptions. User defined classes cannot be directly derived from this class, to derive user defied class, we need to use Exception class. The Python Exception Hierarchy is like below. BaseException.
Type Error Exception is raised when two different or unrelated types of operands or objects are combined. In the below example, an integer and a string are added, which results in a type error. try: a = 5 b = "DataCamp" c = a + b except TypeError: print ('TypeError Exception Raised') else: print ('Success, no error!
What is Python ValueError? Python ValueError is raised when a function receives an argument of the correct type but an inappropriate value. Also, the situation should not be described by a more precise exception such as IndexError.
In contrast, the except Exception as e statement is a statement that defines an argument to the except statement. e in the latter statement is utilized to create an instance of the given Exception in the code and makes all of the attributes of the given Exception object accessible to the user.
I have to do :
raise visa_exceptions.VisaIOError(status)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With