VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-catch-multiple-exceptions-in-one-line-in-python/

⇱ How to Catch Multiple Exceptions in One Line in Python? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Catch Multiple Exceptions in One Line in Python?

Last Updated : 23 Jul, 2025

There might be cases when we need to have exceptions in a single line. In this article, we will learn about how we can have multiple exceptions in a single line. We use this method to make code more readable and less complex. Also, it will help us follow the DRY (Don't repeat code) code method.

Generally to handle exceptions we use try/Except methods to get the exceptions. Previously we use them which makes our code complex and does not follow the DRY method of coding.

Prerequisites: Exception handling in Python

Example

The operation a + strs inside the function func will attempt to add an integer (a) and a string (strs), which is not allowed in Python and will result in a TypeError.

Output:

unsupported operand type(s) for +: 'int' and 'str'

We can write this format in a form of passing the tuple into our except. Here we can write the print statement or any other doings only once and declare exceptions in a single line

Output:

local variable 'res' referenced before assignment
Comment
Article Tags: