Errors Computer says no Types of Errors n

  • Slides: 9
Download presentation
Errors “Computer says no. . ”

Errors “Computer says no. . ”

Types of Errors n Many different types of errors n n Three general categories:

Types of Errors n Many different types of errors n n Three general categories: n n new ones are being invented every day by industrious programming students. . compile-time errors linker errors runtime errors Compiler errors are significantly easier to detect and fix, since the compiler tells you what they are and where to find them!

Compiler Errors n n Compiler is just another program, and requires input in the

Compiler Errors n n Compiler is just another program, and requires input in the right format 3 classes of compile time errors: n n lexical errors: ‘words’ cannot be determined syntax errors: ‘grammar’ is incorrect semantic errors: doesn’t make sense! These types of errors are almost identical to those encountered in spoken and written languages. .

Lexical Errors n Compilers work by breaking the source file into tokens n n

Lexical Errors n Compilers work by breaking the source file into tokens n n English equivalent: n n n when this cannot be done, a lexical error occurs misspelling words, nonsense words, incorrect spacing, etc. . “Thw 3#5 -ta_lbe ishcy+3 i#%!” C example: whilea<130 printf(“hello”);

Syntax Errors n n Most common form of compile error Correct grammar of the

Syntax Errors n n Most common form of compile error Correct grammar of the language has not been applied: n n n English example: n n no semicolon at end of statement no matching ( ), [ ], or { } incorrect structure of if, while, for statements, etc incorrect number of arguments for operators “The dog brown the fence underneath dug” C example: for ( i = 14 ; i++ ){ printf ( “%d”, i )

Semantic Errors n Syntax is correct, but meaning of statements makes no sense n

Semantic Errors n Syntax is correct, but meaning of statements makes no sense n n n C is considerably less strict on semantics than other languages n n incorrect argument types incorrect LHS parameters to certain operators this is a BAD thing for beginner programmers as mistakes are not flagged by the compiler even though they make little sense. . C examples: int a = 14 ; 7=a; printf ( a ) ;

Linker Errors n The C compiler trusts the programmer n n n if they

Linker Errors n The C compiler trusts the programmer n n n if they say a function will be available, the compiler assumes it will be and operates as such sometimes warnings will be generated, sometimes not. . The linker however requires that all such functions actually do exist n n if they do not, errors are generated by the linker these are very different in appearance to compiler errors, and generally are of the form: n n undefined reference to ‘? ? ? ? ’ Can often be fixed by including the correct library or object file, or fixing spelling mistakes in your code

Runtime Errors These errors occur after compilation, when the program is running n They

Runtime Errors These errors occur after compilation, when the program is running n They can either: n n crash the system cause incorrect behaviour The second type are known as ‘logical errors’

Runtime Errors n Typical examples: n n n English equivalent: n n division by

Runtime Errors n Typical examples: n n n English equivalent: n n division by zero accessing invalid memory “Stand up. Lift leg. Lift right leg. ” Logical errors: n anything that isn’t correct!!