You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To make the code more robust and easier to debug, based on python standard, we may update our code based on the following best practises:
Use Exceptions, Not Return Codes
Instead of returning error codes, it's more Pythonic to raise exceptions when something goes wrong. This makes your code cleaner and less cluttered with error-handling code.
Minimize the Amount of Code in try/except Blocks
The more code that is within a try block, the more likely it will be that an exception will occur. This can make it harder to determine what is causing an error. So, limit the amount of code in your try blocks to just the part that might raise an exception.
When catching exceptions, catch specific exceptions instead of just a generic Exception. This allows you to handle different types of exceptions differently and prevents you from catching exceptions that you didn't intend to catch.
When deciding whether to handle an exception or to let it propagate, consider who the caller is. If the caller would be in a good position to handle the exception, it may be better to let it propagate.
Use Finally for Cleanup
If there are resources that need to be cleaned up (like file handles or database connections), consider using a finally block. Code in a finally block will be run whether an exception was raised or not.
If your library or application has errors that it might want to raise, consider defining custom exception classes. This can make it easier for users of your code to handle exceptions appropriately.
The text was updated successfully, but these errors were encountered:
To make the code more robust and easier to debug, based on python standard, we may update our code based on the following best practises:
Instead of returning error codes, it's more Pythonic to raise exceptions when something goes wrong. This makes your code cleaner and less cluttered with error-handling code.
The more code that is within a try block, the more likely it will be that an exception will occur. This can make it harder to determine what is causing an error. So, limit the amount of code in your try blocks to just the part that might raise an exception.
When catching exceptions, catch specific exceptions instead of just a generic Exception. This allows you to handle different types of exceptions differently and prevents you from catching exceptions that you didn't intend to catch.
When deciding whether to handle an exception or to let it propagate, consider who the caller is. If the caller would be in a good position to handle the exception, it may be better to let it propagate.
If there are resources that need to be cleaned up (like file handles or database connections), consider using a finally block. Code in a finally block will be run whether an exception was raised or not.
If your library or application has errors that it might want to raise, consider defining custom exception classes. This can make it easier for users of your code to handle exceptions appropriately.
The text was updated successfully, but these errors were encountered: