How to Catch Multiple Exceptions in One Line in Python? Last Updated : 29 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 PythonExampleThe 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. Python a = 1 strs = "hello" def func(a): res = a + strs print(res) try: func(a) except(TypeError)as e: print(e) except(UnboundLocalError) as e: print(e) 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 Python a = 1 strs = "hello" def func(a): res: a + strs # unboundlocalerror print(res) try: func(a) except(TypeError, UnboundLocalError) as e: print(e) Output:local variable 'res' referenced before assignment Comment More infoAdvertise with us Next Article How to Catch Multiple Exceptions in One Line in Python? S shiv_ka_ansh Follow Improve Article Tags : Python Python-exceptions Practice Tags : python Similar Reads Multiple Exception Handling in Python Given a piece of code that can throw any of several different exceptions, and one needs to account for all of the potential exceptions that could be raised without creating duplicate code or long, meandering code passages. If you can handle different exceptions all using a single block of code, they 3 min read How to catch a NumPy warning like an exception in Python An N-dimensional array that is used in linear algebra is called NumPy. Whenever the user tries to perform some action that is impossible or tries to capture an item that doesn't exist, NumPy usually throws an error, like it's an exception. Similarly, in a Numpy array, when the user tries to perform 3 min read How to Ignore an Exception and Proceed in Python There are a lot of times when in order to prototype fast, we need to ignore a few exceptions to get to the final result and then fix them later. In this article, we will see how we can ignore an exception in Python and proceed. Demonstrate Exception in Python Before seeing the method to solve it, l 3 min read How to log a Python exception? To log an exception in Python we can use a logging module and through that, we can log the error. The logging module provides a set of functions for simple logging and the following purposes DEBUGINFOWARNINGERRORCRITICALLog a Python Exception Examples Example 1:Logging an exception in Python with an 2 min read Exception Handling Of Python Requests Module Python's requests module is a simple way to make HTTP requests. In this article, weâll use the GET method to fetch data from a server and handle errors using try and except. This will help us understand how to manage situations where the request fails or returns an error."url: Returns the URL of the 3 min read Like