Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs
What is Python try except?
Python try except statement allow you to catch the exceptions. It is utilized to test code for a blunder, which is written in the "try" proclamation. Assuming that a blunder is experienced, the substance of the "except" block is run. Try except block allows you to test your code and handle exceptions if any are raised.
1. Try block is used to test a code for errors.
2. Except block is used to deal with errors
3. The else block is used to run/execute the code when there are no errors.
4. Finally block is used to execute the code no matter what result comes.
Try: The code with your desired exception to get. If any exception occurs during an execution it will appear in except block.
Except: This code runs provided that an exception was brought up in the try block. Code executed in this block is very much like typical code: assuming that there is an exception, it won't be naturally gotten (and presumably stop the program).
You can alternatively name explicit kinds of exceptions in the except explanations, in which case the square might be executed assuming one of the named exceptions was the one brought up in try. While naming numerous exceptions, utilize a tuple. It is lawful to have various except articulations, every one of which names various kinds of exceptions.
Else: This code runs when try block doesn’t show any exceptions. Code executed in this block is very much like typical code: assuming there is an exception, it won't be consequently gotten (and presumably stop the program). Notice that on the off chance that the else block is executed, the except block isn't, as well as the other way around. This block is discretionary.
Finally: This code generally executes after different blocks, regardless of whether there was an uncaught exception (that didn't cause an accident, clearly) or a return articulation in one of the different blocks. Code executed in this block is very much like typical code: in the event that there is an exception, it won't be consequently gotten (and presumably stop the program). This block is additionally discretionary.
Some of the common exception errors:
ImportError: Module not found.
KeyboarrdInterrupt: When the user pressed unrequired key.
EOFError: Whenever End-Of-File hit without perusing any information.
ValueError: when a user passes a invalid argument to a function.
IOError: When a file can’t be opened.
Python try except Statement
The Python try except statement executes the code under the “try” statement. In the event, this code doesn't execute effectively, the program will stop at the code that caused the blunder and the "except" statement will run.
The try block permits you to test a square of code for blunders. The except block empowers you to deal with the mistake with a client characterized reaction.
Try except Example
Syntax for try except block:
In this example we haven’t declared python variable.
If we didn't have try with the exception of block in our code, the program would return a error. A standard user might become confused assuming they see an error message.
Since we have try… except blocks, our code knows how to treat an error is experienced.
Multiple Except statement
Whenever the interpreter experiences an exception, it looks into the except blocks related with that try block.
These except blocks might pronounce what sort of exceptions they handle. Whenever the interpreter observes a same exception, except block gets executed.
You can rehash except statements for various sorts of blunders to test for a long time. This is valuable assuming you presume that one of numerous exceptions might be raised however you don't know which one you will experience.
try: print(ourVariable) except NameError: print('ourVariable is not defined') except: print('Error returned')
For this situation, our code returns ourVariable is not characterized in light of the fact that our code returns a NameError.
For example, you might check for a FileNotFoundError to open a document. Checking for quite a long time would guarantee your program could keep running regardless of whether there was an error opening the record you reference.
Try except python: Finally
Yet, imagine a scenario where we need a message to print both assuming an error is returned and, in the event, that no error is found. That is the place where the long last square comes in. the codes will be executed irrespective of what result try except shows.
At last, squares are a valuable pointer that your code has executed. Since they don't separate between whether a code has effectively executed, they are not as ordinarily utilized.
try: print(ourVariable) except: print('ourVariable is not defined') finally: print('Code has been run.')
The code inside the except block executes on the grounds that there is an exception found in our code (ourVariable isn't defined). The code inside the last condition executes too, on the grounds that our code has completed the process of running.
Try except python: Else
By utilizing an else block, you can characterize code that will be run for the situation that no exceptions are raised. This could be utilized to illuminate a client that a program has effectively executed, for example.
try: print('Test') except: print('There is a problem.') else: print('There are no problems.')
No errors will be shown when the code returns.
We learned, try except blocks make it simple to investigate your Python code. These blocks can be helpful while you are trying existing code or composing new code. It guarantees that your program runs accurately and contains no errors.
In this article, you witnessed the use of Else and Finally blocks and use multiple exceptions just by using a single except clause or use multiple except clauses with single try block. Whether to use try except or not it’s completely upon you but it is sure a really good and helpful for finding errors.