Warning: session_start(): open(/tmp/sess_bf6b2b538def76cbe1ffa513cf2bd2e7, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Ways For Using try Without except (Ignoring Exceptions) in Python - LearnShareIT

Ways For Using try Without except (Ignoring Exceptions) in Python

You want to test a block code for an exception using try but don’t want to handle using except. This is not an impossible thing to do. In this article, we show you some easy ways for using try without except (ignoring exceptions) in Python. Scroll down to read!

The pass statement in Python

It is not hard to ignore exceptions in Python. The pass statement is built-in that allows you to only use try in your program.

The pass is considered a placeholder for doing nothing. It replaces a block of code the Python program should have to run after catching an exception. 

Here is the syntax you need to use:

try:
   [code]
except [Kind of exception]:
   pass

In other words, the pass statement does not do anything. The Python program will move to the next line or block of code.

Using try without except (ignoring exceptions) with the pass statement

Below are 2 ways you can use try without except (ignoring exceptions) in Python using the built-in pass statement.

Using except [Kind of exception]

You follow the syntax sample that we have mentioned above to use try without except. You write the exception name in the [Kind of except]  to ignore the specific exception you desire.

Please keep in mind that only the exception that you have added to the [Kind of exception] will be ignored. If the exception is different from the one that you have desired, Python will still stop your program and give you the error message.

For example, here is a code that causes ValueError:

varStr = "this is a string"
int(varStr)

The error message is:

    int(varStr)
ValueError: invalid literal for int() with base 10: 'this is a string'

The reason is, you can not transform a string text to an integer. You use pass to ignore the ValueError but do nothing else:

varStr = "this is a string"

try:
    int(varStr)
except ValueError:
    pass

print(varStr)
print("Type: ", type(varStr))

The int(varStr) will fail, but the Python program does not stop running. The varStr is still a string instead of an int, which leads to the output:

this is a string
Type:  <class 'str'>

Using only except

You don’t have to write the exception name in the [Kind of exception]. The Python program still runs with no issues.

As you write no kind of exception next to the except, the Python program will catch all kinds of errors, including ones that you don’t intend to catch. Some exceptions that you may not want to ignore are KeyboardInterrupt, BaseException, and SystemExit

Here is the sample code:

varStr = "this is a string"

try:
    int(varStr)
except:
    pass

print(varStr)
print("Type: ", type(varStr))

As you see, the program does not catch any kind of exceptions, including ValueError. The varStr still remains as a string, and the output will be:

this is a string
Type:  <class 'str'>

Summary

Using try without except (ignoring exceptions) in Python is totally possible. You will rely on the pass statement to ignore exceptions. Please consider carefully between using except [Kind of exception] and only except!

Leave a Reply

Your email address will not be published. Required fields are marked *