Home » Python

How to ignore Exceptions in Python?

Python | Ignoring Exceptions: Here, we are going to learn how to ignore exceptions and proceed in Python?
Submitted by Sapna Deraje Radhakrishna, on February 01, 2020

What is an Exception?

An exception is an event, which occurs during the execution of a program that interrupts the normal execution of the application. Generally, any application when encountered with a situation that it can not handle (or such implementation is not implemented), the application throws ( or raises in Python) an exception.

The exception that is a runtime, blows the application. However, it is a good practice to always handle the exception immediately than let the application propagate which might terminate the application with a bulk error message.

Handling Exception in Python

The try-except syntax is as follows:

try:
	statements
except Exception1:
	<handle exception 1>
except Exception2:
	<handle exception2>
else:
	print("Nothing went wrong")
finally:
	print("will'be executed regardless if the try block raises an error or not")

Ignoring the exceptions

When you want to ignore an exception, use the key word "pass". Below are few examples,

    try:
	    <do something>
    except:
	    pass
    try:
	    <do something>
    except Exception:
	    pass 


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.