Debug in Python and debug tactics

Tram Ho

This article is for you newbie, not much experience in debugging and reading exception log

1 What is Debug and when to debug

What is a Debug? Debug is the dev search for error locations in order to understand the cause of the error and provide solutions. Debugging usually occurs in the following cases:

  • There was an error causing the exception
  • Error causing wrong data (No exceptions)

2 Apply tactics

1. Case Exception

This case is easy, all exception content is displayed in the console / log file. You just need to read the log and find the location of the error code line. For example, write a function divided by 0 and see how the exception

and error received after run

Read the above error is obvious that log exception have pointed out, the error in the file line number 2. Ez not. Worse still, the long lines of error show raised from the library, took the time to drag and read each line, see which file belongs to the project and maybe it’s the error file (where it prints the whole line causing the error. error). For each exception show itself, the last line is also the most detailed and detailed about the exception of the type and the content of the error message. There are exceptions that do not show the location of the error file in the project, based on the exception type and detail message details + understanding of the project business that can be judged and can be handled like case 2 below.

2. Case causes data errors but no exception

There are exceptions, but not specifically as in case 1 (Do not indicate the file in the project is faulty) or only cause data errors. Previously, with these cases, I always found out, I always thought that it was due to some library error and the library blamed, but actually because of my code, I just did not find and read it carefully. In cases like this, the debugging tool will now need to be used to block gaps. The condition to find out fast is to understand the project architecture, the processing flow.

The tactics here are simple. Rely on mastering the architecture and the operating flow to judge the points that can cause incorrect code errors. If there are too many positions, put debugging intervals to block and check. After finding the error range (Error range is the interval between 2 debug locations that excepted at 2 points that 1 satisfies, 1 does not). From there, debug the next smaller in the range until the line when the error appears, or read each line of code is fine.

Just say this, but to apply it also has to go through a few bugs to understand it

3 Conclusion

That’s all sharing. Many systems require no crash for every exception, so you or the type of try … except Exception. The exception in python is the biggest and most common, so the code you handle this place should give it an extra line of print out what the actual exception is now and based on that, understand what the exception really is. like this

Share the news now

Source : Viblo