Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between syntax error and runtime error?

For example:

def tofloat(i): 
    return flt(i)

def addnums(numlist):
    total = 0
    for i in numlist:
        total += tofloat(i)
    return total

nums = [1 ,2 ,3]
addnums(nums)

The flt is supposed to be float, but I'm confused whether it is a syntax error or a runtime error.

like image 646
Erika Sawajiri Avatar asked Jun 23 '13 15:06

Erika Sawajiri


People also ask

What is difference between syntax and runtime error in Python?

A syntax error happens when Python can't understand what you are saying. A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions.

What is the difference between syntax error and compilation error?

Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are known as syntax errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by compiler and thus are known as compile-time errors.

What is the difference between runtime error and logic error?

Definition. A runtime error is an error that occurs while running a computer program while a logical error is an error in a program that causes it to operate incorrectly, but not to terminate abnormally. This is the main difference between runtime error and logical error.

What is an example of a runtime error?

Here are some examples of common runtime errors you are sure to encounter: Misspelled or incorrectly capitalized variable and function names. Attempts to perform operations (such as math operations) on data of the wrong type (ex. attempting to subtract two variables that hold string values)


2 Answers

Actually, it is a runtime error, because Python will try to resolve the flt name during runtime (because it's a dynamic language), and it won't find it. When this happens, Python yields and exception saying that it couldn't find the symbol you were using flt and all this happens at runtime.

Syntax errors happen when the interpreter find something not compelling with Python's syntax. For example: The Python's grammar doesn't recognize the input syntax as a valid Python program. This may happen when:

  1. You forgot to add : at the end of an if, def, class, etc expression
  2. You forgot to close some parenthesis or brackets, etc.
  3. A lot of places else when you don't adhere to python's grammar :)

In your example, there is nothing wrong with the grammar. For the interpreter, flt(i) is a very valid call to a flt method which had to be check at runtime within the scopes if it really exists. So the interpreter won't complaint and the syntax of your problem is good.

Actually, this can be seen as a disadvantage over compiled languages like C#, C++, etc. This kind of errors can be detected sooner at compile time, and the compiler screams loud when it find it so you can notice it.

With dynamic languages, you won't notice this until the actual method is called. Your program is simple, so you may find it quick. But, what about the missing o in float was inside some legacy framework within a subclass of a subclass of a class, as a property, inside some other module, etc. That would be harsh :)

UPDATE: The execution model in Python's docs are a great read if you're into how does Python internals works. This will clarify your doubt further and will give you a lot of knowledge :)

Hope this helps!

like image 59
Paulo Bu Avatar answered Oct 12 '22 23:10

Paulo Bu


SyntaxError is raised by parser when it founds that your syntax is not correct, like missing colons, parenthesis, invalid statements etc. It'll not allow you to execute your code until you don't fix that the issue.

Your code will throw only error at runtime, i.e when the function tofloat(i) is called for the first time, so it is a runtime error. Specifically NameError.

Also a runtime error won't stop your program's execution until that buggy part is not executed. So, your code can actually run fine if you don't call tofloat ever.

The code below executes properly up to third line but then stops as NameError is raised.(a runtime error)

print 1
print 2
print 3     
print foo

output:

1
2
3
Traceback (most recent call last):
  File "so.py", line 4, in <module>
    print foo
NameError: name 'foo' is not defined

This code won't execute as we made a SyntaxError, even though the first 3 lines are perfectly okay:

print 1
print 2
print 2
print (foo

Output:

$ python so.py
  File "so.py", line 5

              ^
SyntaxError: invalid syntax

Note that there's also a RunTimeError in python, which is raised when an error is detected that doesn't fall in any of the other categories

like image 35
Ashwini Chaudhary Avatar answered Oct 12 '22 23:10

Ashwini Chaudhary