09 Errors, Good Programming Practices, and Debugging.pptx

Document Details

PlentifulMonkey

Uploaded by PlentifulMonkey

Universidad Autónoma de Nuevo León

Tags

debugging programming practices error handling software development

Full Transcript

Errors, Good Programming Practices, and Debugging Syntax Errors Occurs when incorrect syntax is used Python cannot understand the instruction Introductio Example: 1= x is not legal in Python Python returns an...

Errors, Good Programming Practices, and Debugging Syntax Errors Occurs when incorrect syntax is used Python cannot understand the instruction Introductio Example: 1= x is not legal in Python Python returns an error message and n to Error points out the location Types Runtime Errors Logical Errors Syntax Errors Detection Usually easily detectable of Syntax Found in the context of the Errors code Fixing Typically straightforward to Syntax fix Errors Error Shows what happened Message Indicates where the error Details occurs Runtime Errors that occur during Errors execution are called exceptions or runtime errors Exceptions are more difficult to find and are only detectable when a program is run Exceptions in Code Execution Exceptions are not fatal Python will terminate the program if exceptions are not handled Examples of Built-in Exceptions Built-in Exceptions ZeroDivisionError TypeError NameError Complete list available in Python documentation Custom Exceptions Can define your own exception types Documentation provides guidance on customized exceptions Locating Exceptions Python stops running and indicates where the problem is Seasoned programmers run functions multiple times to catch errors No exception does not guarantee correct function Definition of Logic Errors Errors that do not throw an error Program runs smoothly but produces incorrect output Example of Logic Error Logic Incorrect implementation of factorial function Output always 0 due to initialization error Errors Challenges of Logic Errors Easy to generate in long, complex programs Notoriously difficult to find Finding Logic Errors Meticulously comb through each line of code Understand Python's response to every command Techniques to Familiarity with common mistakes is crucial Prevent Errors Introducti on to Learning Process Learning about mistakes is a continuous process Avoiding Cannot list all mistakes Errors Building Good Adopting good habits Habits helps in error prevention Plan Your Importance of Structure Helps in creating a clear direction for the program Program in Programming Ensures that all tasks are addressed in order Results in code full of Consequences of errors Haphazard Planning Leads to longer completion time Define functions as Components of Program sequences of instructions Planning Design modules for repeated tasks Plan from top to bottom Effective Planning Program from bottom to Strategy top Test Each Module Independently Use test cases with known answers Include corner cases in tests Ensure function works properly before moving on Test Within a Single Module Test Perform intermediate tests during coding Everything Identify errors in recent code changes Often Avoid writing large amounts of code without testing Benefits of Frequent Testing Reduces difficulty in finding errors Ensures subsequent modules work correctly Prevents assuming incorrect code is correct Keep Your Code Clean Reduce Reduce Code to Fewer characters reduce the chance of mistakes Minimize Errors Complete expressions improve understanding Use Use Variables Clarifies the purpose of the code Instead of Values Facilitates easy changes Familiariz Familiarize with Makes code more concise e and efficient Common Functions Keep Keep Code Clean Write code in the fewest instructions possible Introduction to Try/Except Assigning Try-Except Commenting Handling Errors Descriptive Statement Code Gracefully Variable Names Execution Use short, Comment Use Try-Except Python attempts descriptive frequently but statements to to execute code names for avoid over- handle errors in the try variables commenting Execute statement Avoid overly Determine alternative If an exception long variable appropriate actions if an occurs, the names level of error occurs except commenting statement is executed Using raise to Raise exceptions in handle exception specific cases s Example: x must be less than or equal to 5 Raising Exceptions Code to raise Raise exception if x is exception larger than 5 Program stops execution and displays exception Handling Different Exceptions Multiple Except Generic Except Statements Statement Handle different Catch any exceptions with exception separate except without statements specifying the type Type Python's Typing Strongly and dynamically typed language Checking Characteristic s Variables can take any data type at any time Dynamic Variables can change types during runtime Typing Example: x = 1 followed by x = 's' Variables cannot change type once assigned Strong Typing Example: '3' + 5 is not allowed Static Typing Data type must be declared before use in Other Languages Data type usage cannot change within function scope Issue with Python Functions No way to ensure user inputs correct data type Example: my_adder function designed to add numbers Example of Problems with Incorrect Data Types Type User can input strings, lists, dictionaries, or functions Checking Causes different levels of problems Solution: Type Checking Check input variables before continuing Force an error using the error function Add a Adding a Breakpoint Breakpoint Insert a breakpoint into your code Python will stop at the breakpoint when the function is run Using pdb.set_trace() Program stops at this line and activates the pdb debugger Check all variable values assigned before this line Use the command 'c' to continue execution Benefits of Python Debugger Helpful in finding and fixing errors Encouraged for use in large programs

Use Quizgecko on...
Browser
Browser