To debug a script, you can use a built-in debugger.
For debugging use only the system instances intended for developing and testing business solutions, where there is no sensitive data, to avoid its loss or corruption.
You can use the try... catch blocks for script debugging.