There are two main types of errors in programming. Syntax errors and Exception errors. Exception errors are usually Runtime errors where the error is only detected as the program is running and an unexpected value is met, for example the user typing a string when the program expects an integer. Whereas Syntax errors are where there is an error in the code that stops the code running such as a missing parameter or command spelt incorrect. The other popular type of error is a semantic error, in the case the program might not work as expected and might not create an error message.
SECTION 1 | SYNTAX ERRORS
A syntax error refers to an error in the structure or "grammar" of a code. It occurs when the code is not written according to the rules and conventions of the programming language being used. Syntax errors prevent a program from being executed or even compiled because the compiler or interpreter cannot understand the code as written. Common examples of syntax errors include missing parentheses, unmatched curly braces, misspelled keywords, or missing semicolons in languages where they are required.
SECTION 2 | RUNTIME ERRORS
Runtime errors are often more difficult to find than syntax error, it could even be that your code works fine most of the time, but when the user does something unexpected it crashes. A complete and comprehensive test plan should be done to try to avoid runtime errors occurring.
SECTION 3 | TRY AND EXCEPT
Remember you should firstly try to write your code so your program does not need to use the try and except method however it does provide a measure you can take to handle runtime errors to help prevent your program from an unexpected exit or even crash.
For example, imagine you build a calculator in python to do division calculations. Your program asks the use to enter the first number, then the second number then outputs the answer of the division. The program would always work fine as long a sensible data is entered by the user. If the user tried to divide a number by 0 then it would cause a run time error. Try the coded below:
def divide (x,y): return (x/y) divide(2,0)
You will get a runtime error (ZeroDivisionError: division by zero) because you cannot divide by zero. This is an example of a Zero Division Error however there are many other common error such as:
To stop the zero division error causing an unexpected stop of the program we can use the try and except method. This will try to run the code, but if it detects an error then it will look to run the the exception. It is always good practice to stipulate the exception, for example:
In the example above the program would still stop running if the user typed in a letter instead of a number, this would be a value error. The program below deals with both the Zero Division Error and the Value Error.
def divide (): try: num1 = int(input("Please enter the first number to divide: ")) num2 = int(input("Please enter the second number to divide: ")) ans = (num1/num2) return (ans) except (ZeroDivisionError,ValueError): return("You cannot divide by Zero\nYou cannot divide a letter")
Whilst you could simply use the except() method and not stipulate the expected exceptions, this is not good practice. Please feel free to copy and paste the code below to try it:
1: The program below will run however it contains a runtime error. Can you identify what is causing the runtime error?
2: What is a syntax error? 3: The code below has 3 syntax errors. Can you find these errors?