My question seems rather simply, but I didn't find any post to this particular issue. I need my own custom exception class deriving from ValueError to print the expected type (standard error msg) as well as the type that was entered (with custom text).
class MyOwnException(ValueError):
...
try:
raise MyOwnException ( int('str') ) #not sure what to do here, as I only want to
#raise the exception if incorrect value type
except MyOwnException as e:
print "Error: Expected type", e.expType() #int
print "Error: Entered type", e.entType() #string
To add to the above and raising my custom exception via the built-in ValueError:
class MyOwnException(ValueError):
def __init__(self, value):
self.value = value
print "Error: Expected type", type(self.value) #int
print "Error type", self.value #how to return expected value type?
try:
int('str')
except ValueError as e:
raise MyOwnException(e)
I would very much appreatiate any help in this regard. Thanks very much!
Cheers, Manuel
It's generally the case that when raising custom exceptions, you have to catch a more generic exception and re-raise a different one. So for example,
>>> class MoofiError(ValueError):
... pass
...
>>> try:
... int('a')
... except ValueError:
... raise MoofiError, 'you did something wrong, fool'
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
__main__.MoofiError: you did something wrong, fool
The int function will always return a ValueError, rather than your custom type.
In order to throw a different exception, you would have to wrap int to catch ValueError and then raise an exception of your choice (which could include the failing value, as you want).
This code creates a new exception class
class MyOwnException(ValueError): pass
However nothing will make someone else's code raise your exception - you can only raise it in yours.
Related
I am trying to raise an exception inside the exception, without getting an error message of the outer exception and the traceback and print the hey only. However, I am a quite stuck here if I put a string instead of k or m. Any ideas?
The output, which I would need without the traceback
def division(k, m):
try:
k / m
except TypeError:
raise ValueError ('hey') from None
return k / m
I'm not exactly sure what you are trying to achieve here, but why don't you just print the string "hey" when the TypeError is raised? Like this:
def division(k, m):
try:
k / m
except TypeError:
print("hey")
return None
return k / m
If you need to raise the ValueError for any reason, then you can catch the ValueError where you call the method, I guess:
def division(k, m):
try:
k / m
except TypeError:
raise ValueError
return k / m
try:
division("not_an_int", "could_be_an_int")
except ValueError:
print("hey")
From what I understand, You want a custom exception with a custom message format.
class CustomException(Exception):
def __init__(self, message):
self.message = message # You can also set it by default so you don't need to input any message
super().__init__(self.message)
def __eq__(self, other):
return self.message == other.message
def __str__(self):
return f'{self.message}' # In your case, just to display message.
# return f'Error message: {self.message}'
def division(k, m):
try:
k / m
except TypeError:
raise CustomException(message="hey")
return k / m
try:
division("not_an_int", "could_be_an_int")
except ValueError as e:
print(e)
It is not entirely clear what you are trying to achieve here. You don't get the error message because you raise the exception but because you don't catch it again. If you don't want the error message, you need to catch the exception. Then, if you just want to print the message 'hey' you can get it from the exception args.
try:
division('a', 2)
except ValueError as e:
print(e.args[0])
The raise ... from None already got rid of the first exception, so you are only seeing the ValueError you raise in the except block in division, but if an exception makes it all the way out to the command line, you have to be informed somehow, and Python will do so by printing the error message.
Now, you can change the default behaviour for uncaught exceptions if you want to. This, for example, will print the args[0] for all exceptions you do not explicitly catch.
import sys
def handle_exception(exc_type, exc_value, exc_traceback):
print('handler:', exc_value.args[0])
sys.excepthook = handle_exception
The sys.excepthook is a function Python will call for an uncaught exception. So if you do
try:
division('a', 2)
except ValueError as e:
print('caught:', e)
division('a', 2) # not caught
the first division exception is caught, and the handler isn't invoked, and the second isn't caught and the handler is used (and will just print the message 'hey').
It is not really a great idea to change the way all uncaught exceptions are handled, though. You probably want to handle only your own and use the default behaviour for anything else.
But, as I said, it is not entirely clear to me what you are trying to achieve, so all of the above might be entirely unrelated to the question.
How do I raise an exception in Python so that it can later be caught via an except block?
How do I manually throw/raise an exception in Python?
Use the most specific Exception constructor that semantically fits your issue.
Be specific in your message, e.g.:
raise ValueError('A very specific bad thing happened.')
Don't raise generic exceptions
Avoid raising a generic Exception. To catch it, you'll have to catch all other more specific exceptions that subclass it.
Problem 1: Hiding bugs
raise Exception('I know Python!') # Don't! If you catch, likely to hide bugs.
For example:
def demo_bad_catch():
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
except Exception as error:
print('Caught this error: ' + repr(error))
>>> demo_bad_catch()
Caught this error: ValueError('Represents a hidden bug, do not catch this',)
Problem 2: Won't catch
And more specific catches won't catch the general exception:
def demo_no_catch():
try:
raise Exception('general exceptions not caught by specific handling')
except ValueError as e:
print('we will not catch exception: Exception')
>>> demo_no_catch()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
Best Practices: raise statement
Instead, use the most specific Exception constructor that semantically fits your issue.
raise ValueError('A very specific bad thing happened')
which also handily allows an arbitrary number of arguments to be passed to the constructor:
raise ValueError('A very specific bad thing happened', 'foo', 'bar', 'baz')
These arguments are accessed by the args attribute on the Exception object. For example:
try:
some_code_that_may_raise_our_value_error()
except ValueError as err:
print(err.args)
prints
('message', 'foo', 'bar', 'baz')
In Python 2.5, an actual message attribute was added to BaseException in favor of encouraging users to subclass Exceptions and stop using args, but the introduction of message and the original deprecation of args has been retracted.
Best Practices: except clause
When inside an except clause, you might want to, for example, log that a specific type of error happened, and then re-raise. The best way to do this while preserving the stack trace is to use a bare raise statement. For example:
logger = logging.getLogger(__name__)
try:
do_something_in_app_that_breaks_easily()
except AppError as error:
logger.error(error)
raise # just this!
# raise AppError # Don't do this, you'll lose the stack trace!
Don't modify your errors... but if you insist.
You can preserve the stacktrace (and error value) with sys.exc_info(), but this is way more error prone and has compatibility problems between Python 2 and 3, prefer to use a bare raise to re-raise.
To explain - the sys.exc_info() returns the type, value, and traceback.
type, value, traceback = sys.exc_info()
This is the syntax in Python 2 - note this is not compatible with Python 3:
raise AppError, error, sys.exc_info()[2] # avoid this.
# Equivalently, as error *is* the second object:
raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
If you want to, you can modify what happens with your new raise - e.g. setting new args for the instance:
def error():
raise ValueError('oops!')
def catch_error_modify_message():
try:
error()
except ValueError:
error_type, error_instance, traceback = sys.exc_info()
error_instance.args = (error_instance.args[0] + ' <modification>',)
raise error_type, error_instance, traceback
And we have preserved the whole traceback while modifying the args. Note that this is not a best practice and it is invalid syntax in Python 3 (making keeping compatibility much harder to work around).
>>> catch_error_modify_message()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in catch_error_modify_message
File "<stdin>", line 2, in error
ValueError: oops! <modification>
In Python 3:
raise error.with_traceback(sys.exc_info()[2])
Again: avoid manually manipulating tracebacks. It's less efficient and more error prone. And if you're using threading and sys.exc_info you may even get the wrong traceback (especially if you're using exception handling for control flow - which I'd personally tend to avoid.)
Python 3, Exception chaining
In Python 3, you can chain Exceptions, which preserve tracebacks:
raise RuntimeError('specific message') from error
Be aware:
this does allow changing the error type raised, and
this is not compatible with Python 2.
Deprecated Methods:
These can easily hide and even get into production code. You want to raise an exception, and doing them will raise an exception, but not the one intended!
Valid in Python 2, but not in Python 3 is the following:
raise ValueError, 'message' # Don't do this, it's deprecated!
Only valid in much older versions of Python (2.4 and lower), you may still see people raising strings:
raise 'message' # really really wrong. don't do this.
In all modern versions, this will actually raise a TypeError, because you're not raising a BaseException type. If you're not checking for the right exception and don't have a reviewer that's aware of the issue, it could get into production.
Example Usage
I raise Exceptions to warn consumers of my API if they're using it incorrectly:
def api_func(foo):
'''foo should be either 'baz' or 'bar'. returns something very useful.'''
if foo not in _ALLOWED_ARGS:
raise ValueError('{foo} wrong, use "baz" or "bar"'.format(foo=repr(foo)))
Create your own error types when apropos
"I want to make an error on purpose, so that it would go into the except"
You can create your own error types, if you want to indicate something specific is wrong with your application, just subclass the appropriate point in the exception hierarchy:
class MyAppLookupError(LookupError):
'''raise this when there's a lookup error for my app'''
and usage:
if important_key not in resource_dict and not ok_to_be_missing:
raise MyAppLookupError('resource is missing, and that is not ok.')
Don't do this. Raising a bare Exception is absolutely not the right thing to do; see Aaron Hall's excellent answer instead.
It can't get much more Pythonic than this:
raise Exception("I know Python!")
Replace Exception with the specific type of exception you want to throw.
See the raise statement documentation for Python if you'd like more information.
In Python 3 there are four different syntaxes for raising exceptions:
raise exception
raise exception (args)
raise
raise exception (args) from original_exception
1. Raise exception vs. 2. raise exception (args)
If you use raise exception (args) to raise an exception then the args will be printed when you print the exception object - as shown in the example below.
# Raise exception (args)
try:
raise ValueError("I have raised an Exception")
except ValueError as exp:
print ("Error", exp) # Output -> Error I have raised an Exception
# Raise exception
try:
raise ValueError
except ValueError as exp:
print ("Error", exp) # Output -> Error
3. Statement raise
The raise statement without any arguments re-raises the last exception.
This is useful if you need to perform some actions after catching the exception and then want to re-raise it. But if there wasn't any exception before, the raise statement raises a TypeError Exception.
def somefunction():
print("some cleaning")
a=10
b=0
result=None
try:
result=a/b
print(result)
except Exception: # Output ->
somefunction() # Some cleaning
raise # Traceback (most recent call last):
# File "python", line 8, in <module>
# ZeroDivisionError: division by zero
4. Raise exception (args) from original_exception
This statement is used to create exception chaining in which an exception that is raised in response to another exception can contain the details of the original exception - as shown in the example below.
class MyCustomException(Exception):
pass
a=10
b=0
reuslt=None
try:
try:
result=a/b
except ZeroDivisionError as exp:
print("ZeroDivisionError -- ",exp)
raise MyCustomException("Zero Division ") from exp
except MyCustomException as exp:
print("MyException",exp)
print(exp.__cause__)
Output:
ZeroDivisionError -- division by zero
MyException Zero Division
division by zero
For the common case where you need to throw an exception in response to some unexpected conditions, and that you never intend to catch, but simply to fail fast to enable you to debug from there if it ever happens — the most logical one seems to be AssertionError:
if 0 < distance <= RADIUS:
#Do something.
elif RADIUS < distance:
#Do something.
else:
raise AssertionError("Unexpected value of 'distance'!", distance)
Read the existing answers first, this is just an addendum.
Notice that you can raise exceptions with or without arguments.
Example:
raise SystemExit
exits the program, but you might want to know what happened. So you can use this.
raise SystemExit("program exited")
This will print "program exited" to standard error before closing the program.
Just to note: there are times when you do want to handle generic exceptions. If you're processing a bunch of files and logging your errors, you might want to catch any error that occurs for a file, log it, and continue processing the rest of the files. In that case, a
try:
foo()
except Exception as e:
print(e) # Print out handled error
block is a good way to do it. You'll still want to raise specific exceptions so you know what they mean, though.
Another way to throw an exception is using assert. You can use assert to verify a condition is being fulfilled. If not, then it will raise AssertionError. For more details have a look here.
def avg(marks):
assert len(marks) != 0, "List is empty."
return sum(marks)/len(marks)
mark2 = [55,88,78,90,79]
print("Average of mark2:", avg(mark2))
mark1 = []
print("Average of mark1:", avg(mark1))
You might also want to raise custom exceptions. For example, if you're writing a library, it's a very good practice to make a base exception class for your module, and then have custom sub-exceptions to be more specific.
You can achieve that like this:
class MyModuleBaseClass(Exception):
pass
class MoreSpecificException(MyModuleBaseClass):
pass
# To raise custom exceptions, you can just
# use the raise keyword
raise MoreSpecificException
raise MoreSpecificException('message')
If you're not interested in having a custom base class, you can just inherit your custom exception classes from an ordinary exception class like Exception, TypeError, ValueError, etc.
If you don't care about which error to raise, you could use assert to raise an AssertionError:
>>> assert False, "Manually raised error"
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
assert False, "Manually raised error"
AssertionError: Manually raised error
>>>
The assert keyword raises an AssertionError if the condition is False. In this case, we specified False directly, so it raises the error, but to have it have a text we want it to raise to, we add a comma and specify the error text we want. In this case, I wrote Manually raised error and this raises it with that text.
You should learn the raise statement of Python for that.
It should be kept inside the try block.
Example -
try:
raise TypeError # Replace TypeError by any other error if you want
except TypeError:
print('TypeError raised')
If I've defined a class that takes in two or more parameters:
class SomeObject:
def __init__(self, int_param, str_param):
#if parameters are wrong type:
raise TypeError("wrong type")
...
if (__name__ == "__main__"):
try:
invalid_obj = SomeObject('two', 'string')
print (invalid_obj)
except TypeError as e:
print (e)
it'll print "wrong type", but is there a way for it to also return which argument is raising the exception? Can I get the output to be:
"wrong type 'two'"
? I've tried print (e.args), print (repr(e)), and simply print(e) but the invalid argument never gets printed. Just wondering if this is possible, thank you.
edit: It works with a custom exception type, and if I list each parameter being tested individually - I also have ValueError catching for both the int_param and str_param as well, so I wanted to see if I can condense the if statements to just:
#if params not right type:
raise TypeError
#if params contain invalid value:
raise ValueError
and still get it to return the specific invalid argument that's causing the error. I'll definitely keep the custom Exception type in mind for future assignments; unfortunately for this one the instructor has "raise TypeError" and "raise ValueError" as requirements for full marks. She doesn't require the invalid argument to be returned though, that was just me being curious if I could manage it. I ended up with:
#if int_param not int:
raise TypeError("int_param {0} given is invalid type".format(int_param))
#if str_param not int:
raise TypeError("str_param {0} given is invalid type".format(str_param))
#if int_param is an invalid value:
raise ValueError("int_param {0} given is invalid value".format(int_param))
....
and so on. If anyone can see a cleaner way to do this (without a custom Exception type), please let me know!
You can make your own exception:
class SomeObjectException(TypeError):
def __init__(self, msg, which):
TypeError.__init__(self, msg)
self.which = which
Then:
class SomeObject:
def __init__(self, int_param, str_param):
#if parameters are wrong type:
raise SomeObjectException("wrong type", int_param)
And:
if (__name__ == "__main__"):
try:
invalid_obj = SomeObject('two', 'string')
print (invalid_obj)
except SomeObjectException as e:
print (e, e.which)
Also see proper way to declare custom exceptions in modern Python.
class SomeObject:
def __init__(self, int_param, str_param):
if type(int_param) != int:
raise TypeError("int_param: wrong type")
if type(str_param) != str:
raise TypeError("str_param: wrong type")
Maybe you should look for the traceback module.
instead of raise TypeError("wrong type") wrap traceback.print_exc() in try, except and see if that's what you looking for.
I'm practicing TDD in Python and came across a problem in testing whether an exception is raised.
Here is my test_phonebook.py with test_add_empty_name_raises_exception which fails.
import unittest
import phonebook
class Test(unittest.TestCase):
def test_add_empty_name_raises_exception(self):
self.assertRaises(ValueError, phonebook.add, "", "1111111111")
if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()
Below is my phonebook.py with the method add which adds the data into the dictionary.
import re
_phonebook = {}
file_name = "phonebook.txt"
def is_valid_name(name):
return re.search(r"([A-Z][a-z]*)([\\s\\\'-][A-Z][a-z]*)*", name) is not None
def is_valid_number(number):
return re.search(r"\+?[\d ]+$", number) is not None
def add(name, number):
try:
if is_valid_name(name) and is_valid_number(number):
_phonebook[name] = number
else:
raise ValueError("Invalid arguments.", name, number)
except ValueError as err:
print err.args
if __name__ == '__main__':
pass
My problem is that the test fails even though it is seen in the console log that there was a ValueError raised within the add method.
Finding files... done.
Importing test modules ... done.
('Invalid arguments.', '', '1111111111')
======================================================================
FAIL: test_add_empty_name_raises_exception (path.to.phonebook.test_phonebook.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path\to\phonebook\test_phonebook.py", line 13, in test_add_empty_name_raises_exception
self.assertRaises(ValueError, phonebook.add, "", "1111111111")
AssertionError: ValueError not raised
----------------------------------------------------------------------
Ran 1 test in 0.002s
How do I solve this? I there something I forgot?
I also tried using the new format for handling exceptions in tests in Python 2.7 but it still hasn't caught the ValueError raising.
def test_add_empty_name_raises_exception(self):
with self.assertRaises(ValueError):
self.phonebook.add("", "1111111111)
I also changed the form of the test case into using lambdas but still no changes.
def test_add_empty_name_raises_exception(self):
self.assertRaises(ValueError, lambda: phonebook.add("", "1111111111"))
I also cleaned my directory and restarted Eclipse Luna and problem still persists.
Possible solution
I was reading the 8.Errors and Exceptions documentation and got to the "Raising Exceptions" part which states that:
If you need to determine whether an exception was raised but don’t intend to handle it,
a simpler form of the raise statement allows you to re-raise the exception:
I added this to the existing add method as such:
def add(name, number):
try:
if is_valid_name(name) and is_valid_number(number):
_phonebook[name] = number
print "Added %s:\t%s" % (name, number)
else:
raise ValueError("Invalid arguments.", name, number)
except ValueError as err:
print err.args
raise
Which caused the test case to pass.
Is this the correct way? To call raise again in the except block?
When you catch an exception (in your except ValueError as err: block), you prevent it from continuing back up the call stack to eventually terminate the program. Essentially, you're saying "I know how to handle this, so no need to panic anyone else."
Re-raising an exception is the proper thing to do if you caught the exception but didn't do so to actually fix anything, for instance, to log that it occurred. Typically, though, one catches an exception in order to correct it.
In your case, you're catching the exception almost immediately after you yourself raised it. Why not put your logging statement in the same else block as the raise? No need for a try: ... except: indent at all.
def add(name, number):
if is_valid_name(name) and is_valid_number(number):
_phonebook[name] = number
print "Added %s:\t%s" % (name, number)
else:
print "Invalid arguments.", name, number
raise ValueError("Invalid arguments.", name, number)
return
I want to catch and handle a specific Exception and I want all others to be raised.
The exception I want to catch is like:
Exception("exception want to catch")
The code I tried below doesn't work. In the first code I want the exception to be raise;
try:
raise Exception("exception don't want to catch")
except Exception("exception want to catch"):
pass
But I wouldn't want an exception raise for this code:
try:
raise Exception('exception want to catch')
except Exception('exception want to catch'):
pass
You should define (or use preexisting) concrete exception classes instead of relying on the strings:
>>> class ExcToCatch(Exception): pass
...
>>> class ExcToNotCatch(Exception): pass
...
>>> try:
... raise ExcToCatch()
... except ExcToCatch:
... pass
...
>>> try:
... raise ExcToNotCatch()
... except ExcToCatch:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
__main__.ExcToNotCatch
Presumably ExcToCatch and ExcToNotCatch mean something more meaningful, so they should be named appropriately.
If you absolutely must rely on the strings, you can obtain the string via str(exception):
>>> try:
... raise Exception('some string')
... except Exception as e:
... print str(e)
...
some string
You can include some logic in the except block to re-raise e when necessary (e.g. when str(e) != 'exception want to catch').
If this exception derives from BaseException (which it seems like it's actually an instance of Exception) you can check the args property. The first should be the string you want to handle:
try:
#stuff
catch Exception as ex:
if ex.args[0] == 'My Exception string':
#do stuff
else:
raise
That said, if you have any control over this library, please go and find the author to have him change it. If you do not, my condolences.