Week 13 Topics
Week 13 Topics
Report
May 5, 2025
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Error Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1 Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Semantic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4 Logical Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Error Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1 Human Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Environmental Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 External Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4 Toolchain Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1
1 Introduction
Programming is inherently prone to errors due to its complexity, human involvement,
and environmental factors. This report provides an in-depth exploration of error types,
their sources, error handling techniques, and strategies for syntax and semantic error
recovery. Each topic is covered with detailed explanations and multiple practical examples
to illustrate real-world applications. The goal is to equip developers, educators, and
students with a thorough understanding of error management in programming.
2 Error Types
Errors in programming can be classified based on when they occur and their nature.
Understanding these types is crucial for effective debugging and robust software develop-
ment.
The compiler reports a syntax error due to the missing semicolon after the printf
statement.
• Example 2: Incorrect Keyword in Python
1 def calculate_sum (a , b ) :
2 retrun a + b # Misspelled ’ return ’
The JavaScript engine detects a syntax error due to the unclosed parenthesis.
• Example 4: Invalid Indentation in Python
1 def print_message () :
2 print ( " Hello ! " ) # Incorrect indentation
Python raises a syntax error because the print statement is not properly indented.
2
2.2 Semantic Errors
Semantic errors occur when code is syntactically correct but produces incorrect or unin-
tended behavior due to logical flaws.
• Example 1: Incorrect Loop Logic in Python
1 def factorial ( n ) :
2 result = 0 # Should be 1 for multiplication
3 for i in range (1 , n + 1) :
4 result *= i
5 return result
6 print ( factorial (5) ) # Outputs 0 , expected 120
3
2.3 Runtime Errors
Runtime errors occur during program execution, often due to invalid operations or exter-
nal conditions.
• Example 1: Division by Zero in Python
1 x = 10
2 y = 0
3 result = x / y # Raises ZeroDi vision Error
4
• Example 1: Incorrect Formula in Python
1 def calculate_area ( radius ) :
2 return 3.14 * radius # Should be 3.14 * radius * radius
3 print ( calculate_area (5) ) # Outputs 15.7 , expected 78.5
Including 0 in the sum may not align with the intended logic.
• Example 4: Miscalculated Tax in Java
1 public class TaxCalculator {
2 public static double calculateTax ( double income ) {
3 return income * 0.05; // Should include tiered rates
4 }
5 public static void main ( String [] args ) {
6 System . out . println ( calculateTax (50000) ) ; // Outputs
2500 , expected complex calculation
7 }
8 }
3 Error Sources
Errors arise from various sources, ranging from human mistakes to environmental factors.
Identifying these sources helps in preventing and mitigating errors.
5
3.1 Human Error
Programmers often introduce errors due to inattention, lack of understanding, or incorrect
assumptions.
• Example 1: Typo in Variable Name (Python)
1 total = 100
2 print ( totla ) # Typo in variable name
6
1 with open ( " large_file . txt " , " w " ) as f :
2 f . write ( " x " * 10**9) # May fail if disk is full
7
• Example 2: Malformed JSON in JavaScript
1 let data = JSON . parse ( ’ {" name ": " Alice " ’) ; // Missing
closing brace
8
The JavaScript engines floating-point handling introduces errors.
• Example 4: Linker Error in C++
1 # include < iostream >
2 extern int undef in ed _f un ct io n () ; // Missing implementation
3 int main () {
4 undefined_fun ct io n () ;
5 return 0;
6 }
9
4 try {
5 throw std :: invalid_argument ( " Invalid input " ) ;
6 } catch ( const std :: invalid_argument & e ) {
7 std :: cout << " Caught : " << e . what () << std :: endl ;
8 }
9 return 0;
10 }
10
27 delete [] arr ;
28 return 0;
29 }
4.3 Logging
Logging records errors for debugging and monitoring.
• Example 1: Python Logging
1 import logging
2 logging . basicConfig ( filename = " app . log " , level = logging . ERROR )
3 try :
4 x = 1 / 0
5 except ZeroDi vision Error as e :
6 logging . error ( " Division error : % s " , e )
11
1 import java . util . logging . Logger ;
2 import java . util . logging . FileHandler ;
3 public class LogTest {
4 public static void main ( String [] args ) throws Exception {
5 Logger logger = Logger . getLogger ( " MyLog " ) ;
6 logger . addHandler ( new FileHandler ( " app . log " ) ) ;
7 try {
8 int x = 1 / 0;
9 } catch ( Ari t h me t i cE x c ep t i on e ) {
10 logger . severe ( " Error : " + e . getMessage () ) ;
11 }
12 }
13 }
12
2 if not age_str . isdigit () :
3 raise ValueError ( " Age must be a number " )
4 age = int ( age_str )
5 if age < 0 or age > 120:
6 raise ValueError ( " Invalid age range " )
7 return age
8 try :
9 print ( process_age ( " abc " ) )
10 except ValueError as e :
11 print ( e )
13
A regular expression validates the email format.
• Example 4: C Input Validation
1 # include < stdio .h >
2 # include < string .h >
3 int validate_input ( char * input ) {
4 for ( int i = 0; input [ i ]; i ++) {
5 if (! isdigit ( input [ i ]) ) {
6 return 0;
7 }
8 }
9 return 1;
10 }
11 int main () {
12 char input [10];
13 scanf ( " % s " , input ) ;
14 if (! validate_input ( input ) ) {
15 printf ( " Invalid input \ n " ) ;
16 } else {
17 printf ( " Valid number \ n " ) ;
18 }
19 return 0;
20 }
14
1 def add (a , b )
2 return a + b # Missing colon , IDE suggests adding it
15
5 } catch ( Nu m b e r F o r m a t E x c e p t i o n e ) {
6 return 0; // Default value
7 }
8 }
9 public static void main ( String [] args ) {
10 System . out . println ( getValue ( " abc " ) ) ; // Outputs 0
11 }
12 }
6 Conclusion
This report has provided a comprehensive analysis of error types, sources, handling tech-
niques, and recovery strategies in programming. By understanding these concepts and
applying the demonstrated techniques, developers can build robust, reliable software. The
examples across multiple languages highlight practical applications, ensuring relevance
for diverse programming contexts.
16