Featured post

4 Layers of AWS Architecture a Quick Answer

I have collected real interview questions on AWS key architecture components. Those are S3, EC2, SQS, and SimpleDB. AWS is one of the most popular skills in the area of Cloud computing. Many companies are recruiting software developers to work on cloud computing.

AWS Key Architecture Components AWS is the top cloud platform. The knowledge of this helpful to learn other cloud platforms. Below are the questions asked in interviews recently.
What are the components involved in AWS?Amazon S3.With this, one can retrieve the key information which is occupied in creating cloud structural design, and the amount of produced information also can be stored in this component that is the consequence of the key specified.Amazon EC2. Helpful to run a large distributed system on the Hadoop cluster. Automatic parallelization and job scheduling can be achieved by this component.Amazon SQS. This component acts as a mediator between different controllers. Also worn for cushioning requirements those are obt…

How to Use Python Try and Except Logic Correctly

In Python, using the Try and Except logic, you can avoid exceptions. Error-free programs save a lot of time. So it reduces defects in the production.  
...


How to Use Python Try and Except Logic Correctly


In Python what to do if specific errors occur is handled by using TRY and EXCEPT.  For this, you need to write some logic to handle this. If the programmer did not do this, then the default is for Python to print an error message and stops execution. 


So the responsibility of a programmer is upfront he/she has to find errors and to handle them correctly. This is possible if you use the TRY and EXCEPT.


Python Syntax for Try and Except.


try:
      c = a/b
except:
      c = 1000000

How 'Try' works here. The above syntax says that the Try ends with ':'. That means, in Python, that is the block of Try.
  • The programmer's logic comes in the Try block.
  • The Except: is another block. That means in this block programmer can specify some value. And that value populates when any error happens.

So far so good.


Try and Except Examples.

Example: 1.

Below is the example to give the expected error in except.

try:
      c = a/b
except ZeroDivisionError:
      c = 1000000


You can specify error name in Except: When the error is other than this the 'C' will be assigned as '1000000'.


Example: 2.

Below is the example to give more than one expected error in except.

try:
      c = a/b
except (ValueError, ZeroDivisionError):
      c = 1000000


Also, there can be many except statements associated with a single try:

Example: 3.


Below is the example to give more than one except.
try:
      c = a/b
except ValueError:
    c = 0
except ZeroDivisionError:
      c = 1000000


And, as was mentioned, a variable can hold the value of the error to be caught:

Example: 4.

Below is the example to assign an expected error to a variable.

k = ZeroDivisionError
try:
      c = a/b
except k:
      c = 1000000


If we left out the exception name, any exception that occurs will be caught and the exception code will be executed:


try:
      c = a/b
except:
      c = 0


Related Posts.

Comments

Popular posts from this blog

Hyperledger Fabric: 20 Real Interview Questions

Python IF Statements Multiple Conditions Examples

Best Machine Learning Book for Beginners