Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of try except in python and tryCatch exception in R language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to solve the abnormal problem of try except in python and tryCatch in R language. I hope you will get something after reading this article. Let's discuss it together.

1. cause

When we need to write a very, very long loop, usually if there is an error in a loop, then the loop behind the whole code will not work.

At this time, imagine that if you hang a loop (parallel) that takes a long time to run on the server, or if you need to run something on your computer all night, you may suddenly have a problem with the program after you click to run and go to bed happily. When I lit up the screen with anticipation the next day and found that it was a big red ERROR, it was estimated that my hair might be thinning again.

So at this point, I wonder if we can run, if there is something wrong with the program, then we can just bypass these problems and proceed to the next cycle.

In fact, there are corresponding solutions to this problem in both Python and R.

2. Try/except in Python

First of all, paste the official documentation:

English document: https://docs.python.org/3/tutorial/errors.html

Chinese document: https://docspy3zh.readthedocs.io/en/lates t/tutorial/errors.html

Here we copy the example in the middle of the document to construct our own example, as shown in the following code.

Def divide (x, y): try: result = x / y except ZeroDivisionError: print ("division by zero!") Except: print ('unknown errorships') Else: print ("result is", result) finally: print ("executing finally clause")

Explain the program logic here: run try first, if:

If you don't report an error, you will jump to else and finally to final.

If the denominator is 0, it will skip to except ZeroDivisionError and then ignore else to the last finally.

For other types of errors, ignore except ZeroDivisionError, then go to except, then ignore else to the last finally

That is to say, finally will run anyway.

Let's verify three inputs:

1) case one

Enter:

Divide (2,1)

Output:

Result is 2.0executing finally clause2) case II

Enter:

Divide (2,0)

Output:

Division by Zero executing finally clause3) the situation is trivial ("2", "1")

Output:

Error! Executing finally clause3. TryCatch in R

Similarly, the tryCatch function in R solves a similar problem.

Please refer to the official documentation: trycatch: Evaluates an expression with the possibility to catch exceptions (DEPRECATED)

Then run a similar program above to see the usage

Divide

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report