Raising errors#
When writing good code, it is beneficial to raise exceptions yourself if you are provided with incorrect input.
Consider the example method calculate_weight
below, which calculates the weight of an object, given that we know its density and volume:
def calculate_weight(density, volume):
"""
Calculates weight of an object, given its density and volume.
Args:
density (int): Density of an object.
volume (int): Volume of an object.
Returns:
int: Weight of an object.
"""
return density * volume
calculate_weight(3, 10)
30
calculate_weight(-4, 5)
-20
If we provide invalid input such as negative density or volume, the method will still give us an answer, because it is following a formula. However, using that incorrect calculation can be harmful in applications. Therefore, we can raise exceptions ourselves using the keyword raise
:
def calculate_weight(density, volume):
"""
Calculates weight of an object, given its density and volume.
Args:
density (int): Density of an object.
volume (int): Volume of an object.
Returns:
int: Weight of an object.
Raises:
ValueError: if density or volume are negative values.
"""
if density < 0:
raise ValueError(
"Invalid density. Density parameter can only take positive numbers"
)
if volume < 0:
raise ValueError(
"Invalid volume. Volume parameter can only take positive numbers"
)
return density * volume
calculate_weight(75, 5)
375
calculate_weight(-4, 50)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[13], line 1
----> 1 calculate_weight(-4, 50)
Cell In[11], line 16, in calculate_weight(density, volume)
2 """
3 Calculates weight of an object, given its density and volume.
4
(...)
13 ValueError: if density or volume are negative values.
14 """
15 if density < 0:
---> 16 raise ValueError(
17 "Invalid density. Density parameter can only take positive numbers"
18 )
19 if volume < 0:
20 raise ValueError(
21 "Invalid volume. Volume parameter can only take positive numbers"
22 )
ValueError: Invalid density. Density parameter can only take positive numbers
Exercise#
You are given the method calculate_area
again to calculate the shuttering area of a concrete rectangular column. The shuttering is a temporary arrangement done for vertical surfaces to support the wet concrete till it attains the desired strength (source: Civilread.com).
If incorrect output is provided to it, the method will return -1
. Instead of that, we would like to raise a ValueError
. Modify the method to raise this exception on incorrect output. Furthermore, update the documentation of the method to account for this change.
def calculate_area(height, length, breadth):
"""
Calculates shuttering area of a concrete rectangular column.
Args:
height (int): Height of the column.
length (int): Length of the column.
breadth (int): Breadth of the column.
Returns:
int: The area or -1 if the input is invalid.
"""
if height <= 0 or length <= 0 or breadth <= 0:
return -1
else:
return (2 * breadth + 2 * height) * length
Solution
def calculate_area(height, length, breadth):
"""
Calculates shuttering area of a concrete rectangular column.
Args:
height (int): Height of the column.
length (int): Length of the column.
breadth (int): Breadth of the column.
Returns:
int: The area.
Raises:
ValueError: if height, length or breadth are non-positive values.
"""
if height <= 0 or length <= 0 or breadth <= 0:
raise ValueError("Invalid height or length or breadth provided.")
else:
return (2 * breadth + 2 * height) * length