How to Throw Error in Python

To throw an error in Python, use the raise keyword. The raise keyword is used to raise an exception. You can define what kind of error to raise and the text to print to the user. The program will be terminated if the error is raised, and to avoid this, we need to handle the error.

import os

if(os.path.isfile("sample.txt")):
  print(" The file is present in the specified Location")
else:
  raise FileNotFoundError("The file not present in the specified location It is an user error")

In this program, we imported the os module. The os module is used for interacting with the operating systems.

To check if an object is a file in Python, use the isfile() function. For example, we checked whether the sample.txt is a file using the isfile() function.

The isfile() is a built-in Python function that checks whether the file passed inside the argument is present in the directory. If it is a file in the directory, then the code prints as the file are present in the specified location.

If the file is not present in the specified directory, the other block is executed. It raises an error under the class name of FileNotFoundError class. The error message is displayed as The file not present in the specified location. It is a user error.

Throw an error using the try block in Python

The try block in Python lets you test a block of code for errors. To catch an error in Python, use the except block.

try:
  with open("sample.txt ", "r") as f:
    data = f.read()
    print(data)
except FileNotFoundError:
  print("The mentioned file is not present in the directory")

Output

The mentioned file is not present in the directory

We opened a text file named sample.txt in the read mode in this program. If the file is present in the specified directory, it opens the file. Inside that, we read all the contents from the file and store it in a variable called data.

Then we print that data. If the file is not present in the specified location, an error is raised called the FileNotFoundError. We have handled this error in except block.

Hence, if the file is opened, then the contents are printed. On the other hand, if the file is not present in the location, an error message is thrown using the exception class.

Python: throwing an error using the assert statement

Python has a built-in assert statement to use assertion conditions in the program. The assert statement has a condition or expression which is supposed to be always True.

import os

assert os.path.isfile("sample.txt"), "The file not present in the specified location"

In this program, we imported the os module. The Assert statement is a user-defines error function. The assert statement creates an exception when the first argument becomes False.

If the first statement is False, then the second argument is displayed as the error message. Hence, an assertion error is created as the sample.txt file is not present in the specified location.

Final words

There are three methods for throwing errors in python:

  1. Using raise keyword
  2. Using try and except block
  3. Using assert statement

That’s it for this tutorial.

Related posts

How to Solve MemoryError in Python

How to Solve EOFError in Python

How to Solve FloatingPointError in Python

Leave a Comment