
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10533 Articles for Python

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

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

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

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

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

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

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

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

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