IC Python API:Error Handling

From Reallusion Wiki!
Revision as of 19:54, 17 February 2020 by Chuck (RL) (Talk | contribs) (Exceptions in Python)

Jump to: navigation, search

Main article: RL Python Samples.

iClone will attempt to parse scripts that are loaded into the environment. In most cases, it does a decent job at catching errors and raising issues within the the Console Log. However, in cases where the errors occur in a loop or sequence, a resulting stack overflow can cause the program to shutdown prematurely. With no chance for a print-out combined with a lack of session logs, the user can find it hard to pin-point the time and point of occurrence of the error. Developers are advised to pay special attention to areas of the code that rely on repeated calls to a set of functions. You can use Python's native Try and Except error handling to debug and help fool-proof exceptionally vulnerable segments of your code.

Exceptions in Python

Python has many built-in exceptions which forces your program to output an error when something in it goes wrong. When these exceptions occur, it causes the current process to stop and passes it to the calling process until it is handled. If not handled, our program will crash. For example, if function A calls function B which in turn calls function C and an exception occurs in function C. If it is not handled in C, the exception passes to B and then to A. If never handled, an error message is spit out and our program come to a sudden, unexpected halt. Having the program come to a halt and give details about the error is much more preferable to a program crash.

Python error handling can be broken down into code blocks:

try The try block lets you test block of code for errors.
except The except block lets you handle the error.
else The else block lets you execute code if no errors were raised.
finally The finally block lets you execute code, regardless of the result of the try and expect blocks.