Tutorials  Articles  Notifications  Login  Signup


RK

Rajan Kumar

Founder at HackersFriend Updated Jan. 20, 2023, 1:39 p.m. ⋅ 2822 views

Python - Raise an exception with message


In order to raise an exception in python you need to use raise keyword followed by type of exception. For example if you want to raise file not found exception with a message that given file doesn’t exists, you can use below syntax:

raise FileNotFoudError(“Given file doesn’t exist”)

 

If you want to raise a type error exception you can use:

raise TypeError(“Given input is of not s valid type”)

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account