I copied and pasted these lines of code from a Python tutorial book. Why does this code not work when I try to run it in PyCharm?
def inputNumber ():
x = input ('Pick a number: ')
if x == 17:
raise 'BadNumberError', '17 is a bad number'
return x
inputNumber()
This is what I got when I run the code:
Pick a number: 17
Traceback (most recent call last):
File "C:/Users/arman/Desktop/Scribble/Hello.py", line 153, in <module>
inputNumber()
File "C:/Users/arman/Desktop/Scribble/Hello.py", line 151, in inputNumber
raise 'BadNumberError', '17 is a bad number'
TypeError: exceptions must be old-style classes or derived from BaseException, not str
You can use standard exceptions:
raise ValueError('17 is a bad number')
Or you can define your own:
class BadNumberError(Exception):
pass
And then use it:
raise BadNumberError('17 is a bad number')
Just inherit from Exception class, then you can throw your own exceptions:
class BadNumberException(Exception):
pass
raise BadNumberException('17 is a bad number')
output:
Traceback (most recent call last):
File "<module1>", line 4, in <module>
BadNumberException: 17 is a bad number
If you want to define a your own error you have to do:
class BadNumberError(Exception):
pass
and then use it:
def inputNumber ():
x = input ('Pick a number: ')
if x == 17:
raise BadNumberError('17 is a bad number')
return x
inputNumber()
You should be raising exceptions as raise as follows BadNumberError('17 is a bad number') if you have already defined BadNumberError class exception.
If you haven't, then
class BadNumberError(Exception):
pass
And here is the docs with information about raising exceptions
Related
Let's try this again as my previous post wasn't that clear. I'm a newbie in Python and I'm working on a school project. However I'm stuck on a small part of code.
#Goal
Raise a ValueError when class is called with the wrong arguments.
Check argument age for float/int type and check if arguments is between 0 and 10.
Example:
class Dog():
def __init__(self, name, age):
self.name = name
def check_arg(age):
if isinstance(age, (int,float)) and age >= 0 and age <= 10:
return age
else:
raise ValueError
self.age = check_arg(age)
Now to check if it works I first put
henry = Dog("Henry", 10)
print(henry.age)
The results is printed: 10
Now I check if it is not true and put:
henry = Dog("Henry", 11)
print(henry.age)
Now I get the following:
Traceback (most recent call last):
File "c:\Folder\test.py", line 17, in
henry = Dog("Henry", 11)
File "c:\Folder\test.py", line 12, in init
self.age = check_arg(age)
File "c:\Folder\test.py", line 10, in check_arg
raise ValueError
ValueError
So it does return a ValueError, but I think the function is handling it wrong. When I return instead of raise ValueError it shows: <class 'ValueError'>
Any tips?
I wish my teacher was as fast as you guys. But he said I could ignore the traceback bit. (spend hours trying to solve this)
raise ValueError()
was correct
I need to store num_of_divisions and num_of_classes in the object School
file1.py
import file1
name_of_school=input("Enter name of Schoool\n")
printschool=f"Your School's name is {name_of_school}"
print(printschool)
try:
num_of_class=int(input("How many class are there in your School?\n"))
except (ValueError, TypeError) as okok:
print("Please Enter a valid number")
else:
if num_of_class<=0:
print("Number cannot be zero or less")
else:
printvalue=f"Number of class in school are {num_of_class}"
print(printvalue)
num_of_divisions=[]
for divisionloop in range(num_of_class):
divisionloop=divisionloop+1
num_of_divisions.append(int(input("Enter number of Divisions for class %d:"%(divisionloop))))
pak=file1.School.mouse(num_of_class, num_of_divisions)
print(pak)
fil2.py
this file below is a module
class School:
def mouse(self, num_of_class, num_of_divisions):
print(num_of_class and num_of_divisions)
self.num_of_class=num_of_class
self.num_of_divisions=num_of_divisions
return num_of_class
Error :
Traceback (most recent call last):
File "ttmain.py", line 24, in <module>
pak=classes.School.mouse(num_of_class, num_of_divisions)
TypeError: mouse() missing 1 required positional argument: 'num_of_divisions'
plus I need mouse to return value of num_of_class and num_of_divisions both
You need to create instance of your School class first and then you can access the mouse function.
schoolObj = file1.School()
return_value = schoolObj.mouse(num_of_class, num_of_divisions)
print(return_value)
I am working on a project for university and I am having trouble with raising errors.
I am supposed to create several functions that will eventually be re-used in even more functions that I am about to create. I am supposed to use Raise ValueError when some specific arguments are being used. Yet, when I use the initial functions to define new functions, the ValueError that gets raised is the initial one and not the new one.
def first_function(arg1):
if isinstance(arg1, tuple):
return True
else:
raise ValueError("This is not a tuple")
def second_function(arg2):
if first_function(arg2):
print("That Is Indeed a Tuple")
else:
raise ValueError("This is really not a tuple")
argument = "(1, 1)"
print(second_function(argument))
# output: ValueError: This is not a tuple
# desired output: ValueError: This is really not a tuple
How can I fix this? I am not supposed to repeat code. I am supposed to keep re-using the functions I build, yet the errors seem to interfere.
So, technically, the "This is not a tuple" error is functioning correctly. The error will stop the code - where the error happens - and report on the stack in that state. This is desirable.
That said, you can use a try/except/finally to catch the first error and pass it back to the second function, which is what I think you're trying to do.
def first_function(arg1):
if isinstance(arg1, tuple):
return True
else:
raise ValueError("This is not a tuple")
def second_function(arg2):
# The try statement wraps the if/then/else
try:
# When you call first_function, it will error
# That error will be recognized and passed to
# second_function's "except" statement
if first_function(arg2):
print("That Is Indeed a Tuple")
# This else is probably not necessary
# Since first_function will either work (return True), or
# raise an error, you will never have a situation where
# "if first_function(arg2):" will ever reach this else
# else:
# raise ValueError("This is not a tuple")
except ValueError:
raise ValueError("This is really not a tuple")
argument = "(1, 1)"
print(second_function(argument))
Traceback (most recent call last):
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 18, in second_function
if first_function(arg2):
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 6, in first_function
raise ValueError("This is not a tuple")
ValueError: This is not a tuple
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 27, in <module>
print(second_function(argument))
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 23, in second_function
raise ValueError("This is really not a tuple")
ValueError: This is really not a tuple
As an aside...
The functional goal you're usually trying to accomplish with this type of design pattern, is to avoid the need to do checks like you're doing in first_function (where it checks if the arg is a tuple).
This design pattern is usually so you can leave the error checking to the main calling method, and all the called methods can just do their thing - raising errors if bad code is sent.
I.e.
def first_function(arg1):
return arg1.append("I should be a list")
def second_function(arg2):
try:
_list = first_function(arg2)
print("arg2 is, indeed, a list")
except Exception as e:
raise ValueError("You did not pass a list (ORIG ERROR: {}".format(e))
argument = "(1, 1)"
print(second_function(argument))
OUTPUT:
Traceback (most recent call last):
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 7, in second_function
_list = first_function(arg2)
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 2, in first_function
arg1.append("I should be a list")
AttributeError: 'str' object has no attribute 'append'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 13, in <module>
print(second_function(argument))
File "/home/rightmire/eclipse-workspace/junkcode/test.py", line 10, in second_function
raise ValueError("You did not pass a list (ORIG ERROR: {}".format(e))
ValueError: You did not pass a list (ORIG ERROR: 'str' object has no attribute 'append'
Functionally, the try/except/finally is used to silence errors. As an example...
def first_function(_list, arg1):
_list.append(int(arg1))
return _list
def second_function():
_list = []
while True:
arg2 = input("Enter an integer:")
try:
_list = first_function(_list, arg2)
print("a list containing {}".format(_list))
except Exception as e:
print ("You did not pass an integer. Try again")
second_function()
OUTPUT:
Enter an integer:1
a list containing [1]
Enter an integer:2
a list containing [1, 2]
Enter an integer:3
a list containing [1, 2, 3]
Enter an integer:r
You did not pass an integer. Try again
Enter an integer:4
a list containing [1, 2, 3, 4]
Enter an integer:
I have seen other posts/videos in order to figure out how to solve this issue I am having but with no success. I am trying to raise an exception if the third parameter(p) is a string datatype but all my attempts in order to achieve this have not been successful and was looking for some help on what I am doing wrong.
class Friends(Ben):
def __init__(self, frank, greg, p):
Ben.__init__(self, frank, greg)
self.p = p
try:
if p == str:
raise TypeError("This is a string!")
except:
print("This not a string")
There are a number of oddities in your piece of code; however, to address the issue of raising an Exception, you use raise to do that, as other answers indicate.
The try/except syntax is used to catch and handle exceptions when they occur. As an example, here is a reworked, standalone version of your code snippet that illustrates this.
class Friends:
def __init__(self, frank, greg, p):
if isinstance(p, str):
raise TypeError(p, "This is a string!")
self.p = p
try:
friends = Friends('Frank', 'Greg', 'dubious_string')
except TypeError as e:
print("Hey, I caught the error!")
# print the exception
print(e)
# raise the exception again
raise e
Output:
Hey, I caught the error!
('dubious_string', 'This is a string!')
Traceback (most recent call last):
File "tmp.py", line 14, in <module>
raise e
File "tmp.py", line 10, in <module>
friends = Friends('Frank', 'Greg', 'dubious_string')
File "tmp.py", line 5, in __init__
raise TypeError(p, "This is a string!")
TypeError: ('dubious_string', 'This is a string!')
I'm not sure what you are trying to do with the class, but I think this is what you are looking for:
#p = 10
p = "some string"
if type(p) == str:
raise Exception("This is a string")
class Ben:
def __init__(self):
pass
class Friends(Ben):
def __init__(self,frank,greg,p):
if isinstance(p, str):
raise TypeError('Why are you giving me strings!')
Ben.__init__(frank,greg)
self.p = p
group = Friends('frank', 'greg', 'evil_string')
Output:
Traceback (most recent call last):
File "C:\Users\StackOverflow\Desktop\temp.py", line 15, in <module>
group = Friends('frank', 'greg', 'evil_string')
File "C:\Users\StackOverflow\Desktop\temp.py", line 10, in __init__
raise TypeError('Why are you giving me strings!')
TypeError: Why are you giving me strings!
I hope this is not a naive question. The following is the code:
>>> print(55/0)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
I understand that python has the traceback object, which is a kind of exception object I guess. My question is: what is the meaning of
"in <module>" in the second line? Which module is it? Does it mean in the current module always? It does not provide any useful information, right?
Many thanks for your time and attention.
After trying the following code, I finally understand it by myself.
def get_age():
age = input("Please enter your age: ")
if not age.isdigit():
my_error = TypeError("{0} is not integer".format(age))
raise my_error
elif age < 0:
# Create a new instance of an exception
my_error = ValueError("{0} is not positive".format(age))
raise my_error
return age
def a():
get_age()
def b():
a()
def c():
b()
def d():
try:
c()
print("haha")
except ValueError:
print("no one has negative age, you fool")
#except TypeError:
#print("you should input an integer, dear")
d()
The following is the error message when I did not handle the TypeError.
RESTART: the_path_to_my_python_file.py
Please enter your age: th
Traceback (most recent call last):
File "the_path_to_my_python_file.py", line 33, in <module>
d()
File "the_path_to_my_python_file.py", line 26, in d
c()
File "the_path_to_my_python_file.py", line 22, in c
b()
File "the_path_to_my_python_file.py", line 18, in b
a()
File "the_path_to_my_python_file.py", line 14, in a
get_age()
File "the_path_to_my_python_file.py", line 5, in get_age
raise my_error
TypeError: th is not integer
You see that it does not tell you "in module" anymore. Each time it tells you the name of the function in which the error occurs. I think that is the kind of information "in module" would provide.