- 5/16/2025
Category
📚
LearningTranscript
00:00Debugging Programs
00:11In this topic, we are going to discuss
00:13Introduction to Programs and Their Bugs
00:16Debugging
00:17Errors and Exceptions
00:19Types of Errors
00:21Exception and Exception Handling
00:23Debugging Techniques
00:25Breakpoints
00:26Integrated Debugger Tool
00:28Spider
00:29PDB Python Debugger
00:31And at the end, we will recapitulate the topic.
00:36A computer program is a set of instructions that can be executed by a computer in order to perform a specific task or to produce some output as result.
00:45There are times when the programs don't run successfully because of the bugs and exceptions.
00:50These situations lead the programmer to dedicate additional time to resolve the bugs.
00:55For successful execution of a program, bugs need to be removed through debugging techniques.
01:03Debugging is a process of checking the code, finding the errors, identifying the cause of it, and resolving the errors depending on the type.
01:12But for this, we must know the types of errors and exceptions on which debugging techniques are applied.
01:17Let us first understand errors and exceptions.
01:22Errors and exceptions both prevent program from producing the desired output.
01:27An error can be anything in code that stops the code from compiling and running properly.
01:32And an exception is irregular and unexpected situation occurred during execution of program.
01:38Let us see types of errors.
01:42There are mainly three types of errors in computer programs.
01:46Compile time errors.
01:48Runtime errors and logical errors.
01:51Compile time errors are further divided in syntax errors and semantics error.
01:56Syntax errors.
01:57A syntax error can be defined as a mistake done in writing the source code.
02:05It can be a spelling mistake or any formal rule violation within the code.
02:10Observe the statement given.
02:12This statement is syntactically incorrect.
02:15As if, we compare this with the correct structure of for loop, the range should be written inside the parentheses and should be followed by colon sign at the end.
02:27Semantics error are when code of statement is correct but does not produce any meaningful result.
02:33Semantics errors are not caught by compiler because in this case, the syntax of source code is correct.
02:39In example, the statement is correct but not meaningful as it is asking the user to input a numeric value and whatever value user will input will be string as no int type is written before input.
02:54Errors which occur during the execution of a program are called runtime errors.
02:59Runtime errors cause the program to terminate abnormally or unexpected.
03:03For example, if a loop does not find the terminating condition and goes in infinite state.
03:10Logical errors.
03:12Logical errors are not identified at compile time or runtime.
03:16It is identified at the time of testing when the code is technically correct but does not give the desired result.
03:23For instance, an incorrectly implemented algorithm.
03:26Here is this example.
03:28The code is expected to produce average of two number but only second number is being divided by two, which will not give correct result.
03:39Exception handling.
03:40Exception handling involves responding to the exceptional conditions which require special processing.
03:46In Python, we use try and accept clauses to perform exception handling.
03:50Exception handling is time-taking process as it requires more code.
03:55And also, the programmer needs to think with all possible, correct and incorrect inputs can be given users.
04:04Program needs to develop the code as per program's requirement.
04:08Here are some predefined exceptions which can be directly used in a program.
04:12EO error.
04:13When built-in function hits end of file condition.
04:16IO error.
04:17When I or O operations fail.
04:20Name error.
04:21When identifier name is not found.
04:24Index error.
04:25When sequence of index is out of range.
04:28Import error.
04:29When import statement fails, finding the module.
04:32Type error.
04:33When object type in a function is inappropriate.
04:36Zero division error.
04:38When a value is divided by zero.
04:41Let us see some debugging techniques.
04:45Along with the development, debugging techniques have also changed.
04:49Traditional debugging techniques involve.
04:52Spotting the origin in the code.
04:54Printing variables values.
04:55Code tracing and stepping.
04:57And, modern debugging techniques involve use of debugger tools.
05:04Debugging tools.
05:05A debugging tool is a specialized program that is created to test and debug the program written in a specific programming language.
05:13Integrated debuggers are built-in programs inside the IDEs.
05:19These programs are integrated for specific programming languages.
05:23Let us see a debugger of Spyder IDE, which you use for Python programming.
05:28First of all, make sure that you have opened Variable Explorer, that you can do as shown in the image.
05:36Go to View, then select Panes.
05:39And under Panes, click on Variable Explorer to make it visible or invisible on your IDE screen.
05:45Here is an example of Variable Explorer, where three variable and their values are being showed.
05:53And to turn the debugger on, go to Debug menu, and under this, select the first option, Debug.
05:59It will make the integrated debugger to start debugging the code that you have written in Editor pane.
06:07Observe the code written.
06:09By default, the debugging starts from the beginning of the code, and debugged code shows in Python console.
06:15In the second picture, the debugged code is shown in console.
06:21If you want to debug any specific line of code in the program, then select the line and select Step command under Debug menu,
06:28as shown in first picture.
06:30And, if you want to debug the program step by step, keep pressing Ctrl key with F10 function key on your keyboard.
06:37And to stop the debugging process, you can press together Ctrl key and Shift key with F12 function key,
06:44or can give Stop command in Debug menu.
06:49Breakpoint
06:50A breakpoint is intentional pause or halt given inside a program to mark the debugging points.
06:56It means the program will execute in a normal flow from top to bottom and will halt at breakpoint.
07:04To give breakpoints in the program, just double-click on the line number of the code.
07:08A small red circle will appear to show the breakpoint, and the same way it can be removed.
07:14A breakpoint also can be created by using Set clear breakpoint option under Debug menu, as shown in picture.
07:21PDB is an interactive source code debugger for Python programs.
07:27It supports breakpoints and single-stepping in debugging.
07:30One needs to import the PDB module in order to do interactive debugging.
07:35And also we write PDB.set underscore trace function from where we want the code to start the debugging.
07:44This is a Python code which calculates compound interest.
07:47Here, we have imported PDB in the beginning of the code, and have written the PDB.set underscore trace function in line number 8.
07:55Hence, the tracing will start from here.
08:00Hence, in console, the debugging code will be shown, and the line being debugged will be shown with the marker arrow.
08:09While PDB debugging, also, you can set the breakpoints to define where to start debugging from.
08:14For doing this, you just need to write B followed by code line number from the editor pane.
08:21We can also clear the breakpoints created by using CL command.
08:25And to quit the debugger, just need to give Q command as shown in given screenshot.
08:31Let us recapitulate the topic.
08:33Debugging is technique of checking the code, finding the errors, identifying the cause of it, and resolving the errors depending on the type.
08:41There are three types of errors.
08:441. Compile time error.
08:462. Runtime error.
08:483. Logical error.
08:50PDB is a Python debugger tool.
08:53Logical errors are not found by compiler, but are mainly found at the time of testing process.
09:00A debugger comes with an IDE is called integrated debugging tool.
09:05An exception is an irregular situation faced by program, on which code does not have any control.
09:11An unhandled exception causes a halt in execution of the program.
09:41An unhandled exception does not have any control.
09:42O
09:46or a
Recommended
1:13
|
Up next
5:40
2:11
1:09
24:33
7:46
7:54
9:10
16:42
16:38
9:34
7:52
10:07
6:38
6:19
19:04
8:02
13:05
15:17
10:32
11:11
9:57
5:08
7:20
5:48