Start To Learn Python Right Now EP12: Try and Except Blocks in Python

Start learning Python : Try and Except Blocks in Python by SoftEXS.com

 In this post, we are going to learn about Python Try and Except block. This is a very common function when we come to commercial applications. Because this function is basically used to debug your program codes inside an especially block and return an Exception or next step when your main program raises an error. This very uses full to keep your program alive without getting break or crash when getting an error.  


Try and Except Blocks

Try and accept is mainly working on three basic blocks. They are try, except and finally.

  •                  Try - This is used to check your code whether it is working or not.
  •                  Except - This is used to get the error exception or run the next step when your code went wrong inside the try block.
  •                  Finally - This is the last block we are using in this block pattern. And this is used to return the final command/task/program whether your code is working or not. This is not caused if a try is true or false and it just returns a final command

Note: You need to keep in mind that you can't just use try, you might need to use except with the try block. However, there is no recommendation to use finally with the try and except. 


How to use Try and Except...


     If you run this code you'll see, it is returning the exception because we tried to print variable s, but we don't have a variable called s and it is not defined. In this way, you can debug any of your codes without breaking whole the program.
 

How to get an Exception for a Specific error...

     You were also able to get an exception for a specific error. It is very useful when identifying errors that your code really has. Otherwise, you have to check whole the program by wasting your time. 
   
    You can do it by adding an exception modifier. Here are some examples of the Errors types. You can use them according to the code and category of your program.  

     StandardError - This error is raised by a stop iteration. It means mostly an error of loops like FOR.

    OverflowError - This error is raised by reaching a maximum value of a calculation. This is very helpful when making a simple Python Calculator.

    ImportError - This error is rasing when there is a module importing error or module not found error.  

   NameError - This error is raised when there is a variable error or value not found error.

   RuntimeError - This error is raised when the error does not belong to any category.

Note; There are more error types that you can return as an exception. 

In here I'm using NameError and  TypeError. but you'll see the final exception is  NameError. because 'a' variable is undefined.   Also, You can put an unlimited number of exceptions to a  try block.

How to use Finally with Try and Except...

     Finally is used to execute a final task whether the try is working or not.  It is a helpful block to keep your program alive even the try is failed. 


If you have any problems about this post, feel free to comment them below. 

Post a Comment

If you have any doubt, Let us know

Previous Post Next Post