Syntax, Runtime and Logic Errors
A report can fail—or finish with the wrong answer
A teacher expects average 37.5 from marks 40 and 35. One program cannot parse, another accesses a missing record, and a third runs but divides the total by the wrong count.
A syntax error breaks the language grammar. A runtime error occurs while operations execute. A logic error produces behavior that differs from the intended rule, possibly without throwing.
Classify a symptom
Separate parsing failure, runtime exception and incorrect result.
Compare results
Use a known school example to expose a wrong denominator.
Choose evidence
Match inspection and tests to the failure being investigated.
Three different failure points
Before execution
Source fails grammar checks.
SyntaxDuring execution
An operation cannot proceed as requested.
RuntimeAgainst the requirement
A completed calculation violates the task.
Logic
An exception is not the only evidence of a mistake
A missing closing bracket can prevent JavaScript from parsing a program. Accessing a property on undefined can throw when execution reaches it. Dividing a valid total by three instead of two can complete normally but be wrong.
Classify the observed symptom before editing code. Record the exact input, expected result and actual result; an error message is useful evidence but not always the underlying cause.
Before starting, review averages and grouping. These examples use JavaScript; other languages can use different syntax and rules.
Compare a wrong average with the required average
The total is 75. Dividing by three produces 25, but there are two marks, so dividing by marks.length produces 37.5.
Both expressions are valid and execute. The school requirement distinguishes the correct result from the logic error.
JavaScript · local teaching example
const marks = [40, 35];
const total = marks[0] + marks[1];
console.log(total / 3);
console.log(total / marks.length);Expected output
25
37.5Observe a handled missing-record failure
The selected array position is absent, so student is undefined. Reading student.name throws a TypeError. The catch prints the error category rather than relying on version-specific message wording.
This is a runtime failure; a static or validation check might prevent it, but this source is syntactically valid.
JavaScript · local teaching example
const students = [];
try {
const student = students[0];
console.log(student.name);
} catch (error) {
console.log(error.name);
}Expected output
TypeErrorApply the rule to a small change
Fix a two-subject average for marks 25 and 30 by using the actual list count.
Worked answer
The total is 55 and the average is 27.5. Checking an empty marks list is a separate requirement because a zero denominator is not a useful average.
JavaScript · local teaching example
const marks = [25, 30];
const total = marks[0] + marks[1];
console.log(total / marks.length);Expected output
27.5Recognize mistakes and limits
JavaScript Number division by zero is not necessarily a thrown exception; it can produce Infinity or NaN. Check the result and preconditions rather than expecting catch to enforce every mathematical rule.
One symptom can have an upstream cause such as invalid input or a wrong record selection. Do not change unrelated code merely to silence an exception. Reproduce, isolate and confirm the cause before fixing it.
For the language or tool rules used here, see JavaScript errors and handling.
Check the core idea
Apply the idea to a new situation
Name the symptom and test the intended rule
Syntax, runtime and logic failures occur at different levels. Use exact inputs, results and error evidence to identify the cause; do not equate “no exception” with a correct report.
Connect this idea with handling thrown failures and finding the cause.
Reaching the recap and completing the quizzes are separate milestones.