Found 10533 Articles for Python

How to catch TypeError Exception in Python?

Manogna
Updated on 12-Feb-2020 10:51:23

647 Views

TypeErrors are caused by combining the wrong type of objects, or calling a function with the wrong type of object.Exampleimport sys try : ny = 'Statue of Liberty' my_list = [3, 4, 5, 8, 9] print  my_list + ny except TypeError as e: print e print sys.exc_typeOutputcan only concatenate list (not ""str") to list

How to catch IndentationError Exception in python?

Manogna
Updated on 12-Feb-2020 10:43:35

1K+ Views

A IndentationError occurs any time the parser finds source code that does not follow indentation rules. We can catch it when importing a module, since the module will be compiled on first import. You can't catch it in the same module that contains the try/except block, because with this exception, Python won't be able to finish compiling the module, and no code in the module will be run.We rewrite the given code as follows to handle the exceptionExampletry: def f(): z=['foo', 'bar'] for i in z: if i == 'foo': except IndentationError as e: print eOutput"C:/Users/TutorialsPoint1/~.py", line 5 if i ... Read More

How to catch SyntaxError Exception in Python?

Sarika Singh
Updated on 23-May-2025 12:57:47

2K+ Views

SyntaxError in Python occurs when the interpreter encounters invalid syntax, such as missing colons, unmatched parentheses, incorrect indentation, or invalid keywords. Since this error happens during the code compilation stage (before execution), it cannot be caught using a regular try-except block. To handle it, you must wrap the faulty code inside the exec() or compile() functions within a try-except block. Here, we are demonstarting the occurence of SyntaxError and handling it using the following methods - exec() Method compile() Method Using a custom function with exception handling ... Read More

How to catch IndexError Exception in Python?

Sarika singh
Updated on 22-May-2025 17:11:18

4K+ Views

In Python, an IndexError occurs when you try to access a position (index) in a list, tuple, or similar collection that isn't there (does not exist). It means your program is trying to access the elements that are not available in the sequence (object). Using try-except to Catch IndexError You can use a try-except block to catch (handle) an IndexError and stop your program from crashing if you try to access an index that doesn't exist (or invalid). Example In this example, we try to access the 5th index of a list that only has 3 elements, which causes an ... Read More

How to catch IOError Exception in Python?

Sarika Singh
Updated on 22-May-2025 16:57:13

8K+ Views

In Python, an IOError (or OSError in latest versions) occurs when an input/output operation fails. For example, when we are trying to read a file that doesn’t exist, writing to a file that is read-only, or accessing a corrupted device. You can catch IOError using a try-except block to handle file input/output errors in Python. For compatibility with Python 3, we need to use OSError or catch both using a tuple. Using try-except Block You can catch IOError using a try-except block. This helps you handle file-related errors without crashing the program. Example In this example, we try to open ... Read More

Where can I find good reference document on python exceptions?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

87 Views

The following link is a  good reference document on python exceptionshttps://docs.python.org/2/library/exceptions.html

Why are Python exceptions named \"Error\" (e.g. ZeroDivisionError, NameError, TypeError)?

Sarika Singh
Updated on 22-May-2025 15:31:32

163 Views

In Python, exception names usually end with "Error" (like ZeroDivisionError, NameError, and TypeError). This clearly shows that they are related to problems that happen while the program is running. Using this naming style makes error messages easier to read, helps with debugging. Why exceptions end with "Error" An exception is a kind of (run-time) error. Having the word "Error" in the name of the exception may help us realize that there is an issue when we encounter an exception. It follows a logical naming convention similar to other programming languages like Java and C++, that also use names ending in ... Read More

Explain Try, Except and Else statement in Python.

Sarika Singh
Updated on 16-May-2025 19:45:28

1K+ Views

In Python, the try, except, and else statements are used to handle exceptions and define specific blocks of code that should execute based on whether an error occurs or not. This helps you manage errors and separate successful code from error-handling logic. Using "try" and "except" The try block contains code that might raise an exception. If an exception occurs, the control jumps to the except block, which contains code to handle that exception. Example: Handling division by zero In the following example, we are dividing a number by zero, which raises an exception that is handled by the except ... Read More

How do you properly ignore Exceptions in Python?

Rajendra Dharmkar
Updated on 27-Sep-2019 07:10:07

555 Views

This can be done by following codestry: x, y =7, 0 z = x/y except: passORtry: x, y =7, 0 z = x/y except Exception: passThese codes bypass the exception in the try statement and ignore the except clause and don’t raise any exception.The difference in the above codes is that the first one will also catch KeyboardInterrupt, SystemExit etc, which are derived directly from exceptions.BaseException, not exceptions.Exception.It is known that the last thrown exception is remembered in Python, some of the objects involved in the exception-throwing statement are kept live until the next exception. We might want to do ... Read More

How to use the ‘except’ clause with multiple exceptions in Python?

Sarika Singh
Updated on 16-May-2025 14:47:49

3K+ Views

Using "except" Clause with Multiple Exceptions It is possible to define multiple exceptions with the same except clause. It means that if the Python interpreter finds a matching exception, then it will execute the code written under the except clause. Syntax In general, the syntax for multiple exceptions is as follows - Except(Exception1, Exception2, …ExceptionN) as e: When we define the except clause in this way, we expect the same code to throw different exceptions. Also, we want to take action in each case. Example In this example, we are trying to add an integer and a string, which is not ... Read More

Advertisements