Skip to content

Commit 7e85caa

Browse files
committed
fix: remove generic exception and add ValueError for negative mass in newtons_second_law
1 parent 6328322 commit 7e85caa

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

physics/newtons_second_law_of_motion.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,22 @@
6464

6565
def newtons_second_law_of_motion(mass: float, acceleration: float) -> float:
6666
"""
67-
Calculates force from `mass` and `acceleration`
67+
Calculates force from `mass` and `acceleration`.
68+
Raises ValueError if mass is negative.
6869
6970
>>> newtons_second_law_of_motion(10, 10)
7071
100
7172
>>> newtons_second_law_of_motion(2.0, 1)
7273
2.0
74+
>>> newtons_second_law_of_motion(-5.0, 10)
75+
Traceback (most recent call last):
76+
...
77+
ValueError: Mass cannot be negative
7378
"""
74-
force = 0.0
75-
try:
76-
force = mass * acceleration
77-
except Exception:
78-
return -0.0
79-
return force
79+
if mass < 0:
80+
raise ValueError("Mass cannot be negative")
81+
82+
return mass * acceleration
8083

8184

8285
if __name__ == "__main__":

0 commit comments

Comments
 (0)