2.4 Errors and warnings

¡Supera tus tareas y exámenes ahora con Quizwiz!

Compiler warnings

A compiler will sometimes report a warning, which doesn't stop the compiler from creating an executable program but indicates a possible logic error. Ex: Some compilers will report a warning like "Warning, dividing by 0 is not defined" if encountering code like: totalItems = numItems / 0 (running that program does result in a runtime error). Even though the compiler may create an executable program, good practice is to write programs that compile without warnings. By default, Java compilers don't print every possible warning. Ex: A programmer's Java code may use something old that has a newer, better alternative. The compiler recognizes this, but since the code may still run fine, no warning is given. So, many programmers recommend the good practice of configuring compilers to be more picky with warnings than the default. Ex: javac can be run as javac -Xlint yourfile.java to enable all recommended warnings. Programmers often feel more confident about code that compiles with no warnings when -Xlint is used.

Successfully compiling means the program doesn't have compile-time errors, but the program may have other kinds of errors.

A logic error, also called a bug, is an error that occurs while a program runs. For example, a programmer might mean to type numBeans * numJars but accidentally types numBeans + numJars (+ instead of *). The program would compile but would not run as intended.

Logic errors

Because a syntax error is detected by the compiler, a syntax error is known as a type of compile-time error.

If the programmer corrects an error on line 7, the programmer should

Compile After finding the first error, the programmer should compile. Examining other errors often wastes time, since those may have stemmed from the first.

System.out.print("Dogs: " numDogs);

Error Missing + before numDogs

int numCats numCats = 3; System.out.print(numCats);

Error Missing a semicolon after int numCats.

System.out.print("Hello friends!);

Error Missing the ending ", as in: System.out.print("Hello friends!");

System.print(numDogs);

Error Should be System.out.print, not System.print.

system.out.print("Everyone wins.");

Error Should be an uppercase S in System.

Find the syntax errors. Assume variable numDogs has been declared. System.out.print(numDogs).

Error Statements end with a semicolon, not period.

System.out.print(NumDogs);

Error The declared variable was numDogs, not NumDogs. The compiler will report that NumDogs has not been declared.

When a compiler says that an error exists on line 5, that line must have an error.

False The error may actually exist in an earlier line, but the compiler didn't get confused until reaching line 5.

A compiler's default settings cause most warnings to be reported during compilation.

False Default settings commonly do NOT report all important issues. Good practice is to explicitly configure the compiler to report most warnings.

If a compiler generates a specific message like "missing semicolon", then a semicolon must be missing somewhere, though maybe from an earlier line.

False The actual error could be different, like missing parentheses. If a programmer makes a mistake, the statement and subsequent statements may still be valid code, but eventually the compiler cannot make sense of the code and generates an error message.

If a compiler says that an error exists on line 90, the actual error may be on line 91, 92, etc.

False The actual error may be in an earlier line, but the compiler wasn't able to realize the error immediately, so proceeds to subsequent lines, then gets confused and reports an error.

A compiler warning by default will prevent a program from being created.

False Unless the programmer specially configures the compiler, warnings will NOT prevent a program from being created; only errors will.

Compiling frequently

Good practice, especially for new programmers, is to compile after writing only a few lines of code, rather than writing tens of lines and then compiling. New programmers commonly write tens of lines before compiling, which may result in an overwhelming number of compilation errors and warnings and logic errors that are hard to detect and correct. 1. Writing many lines of code without compiling and running is bad practice. 2. New programmers should compile and run programs after every few lines. Even experienced programmers compile and run frequently.

System.out.print("Amy // Michael");

No Error Some might think // creates a problem, but the compiler does not treat // as a comment when within a string literal.

Syntax errors

People make mistakes. Programmers thus make mistakes—lots of them. One kind of mistake, known as a syntax error, is to violate a programming language's rules on how symbols can be combined to create a program. An example is forgetting to end a statement with a semicolon. A compiler generates a message when encountering a syntax error. The following program is missing a semicolon after the first output statement.

Fixing the first error

Some errors create an upsettingly long list of error messages. Good practice is to focus on fixing just the first error reported by the compiler and then recompiling. The remaining error messages may be real but are more commonly due to the compiler's confusion caused by the first error and are thus irrelevant. 1. Focus on FIRST error message, ignoring the rest. 2.Look at reported line of first error message. If error found, fix. Else, look at previous few lines. 3. Compile, repeat.

System.out.print("Especially '); Invalid closing used single quote (') instead of double quotes

System.out.print("Especially ");

System.out.println("Num is: " - userNum); For string concatenation need to use plus (+) here hypen (-) is used

System.out.println("Num is: " + userNum);

Retype the statements, correcting the syntax errors. System.out.println("Num: " + songnum); Error in songnum, should be songNum

System.out.println("Num: " + songNum);

Correct the syntax errors System.out.printl("Predictions are hard."); This statement missing n for the println()

System.out.println("Predictions are hard.");

System.out.println("about the future."). statement is terminated with dot (.) instead of semicolon (;)

System.out.println("about the future.");

System.out.println(songNum " songs"); Error missing + between songNum and "songs"

System.out.println(songNum + " songs");

System.out.println(int songNum); Error using int should be (songNum)

System.out.println(songNum);

Misleading compiler error message. (VIEW IMAGE)

The compiler indicates a missing semicolon ';'. But the real error is the missing parentheses. Sometimes the compiler error message refers to a line that is actually many lines past where the error actually occurred. Not finding an error at the specified line, the programmer should look to previous lines.

Bugs

The term bug to describe a runtime error was popularized when in 1947 engineers discovered their program on a Harvard University Mark II computer was not working because a moth was stuck in one of the relays (a type of mechanical switch). They taped the bug into their engineering log book, still preserved today (The moth).

Generally, a programmer should not ignore warnings.

True Ignoring warnings can cause an important warning to be overlooked in the middle of less-important warnings. Writing code that generates no warnings is better.

A new programmer writes 5 lines of code, compiles and runs, writes 5 more lines, and compiles and runs again. The programmer is _____ .

following good practice Though beginning programmers may think such an approach wastes time, in fact the approach saves time. The approach enables faster detection and correction of errors. If multiple errors exist in large code, the errors may interact and be harder to detect and correct.

An experienced programmer writes 80 lines of code and then compiles and runs. The programmer is probably _____ .

programming dangerously Even experienced programmers tend to compile and run frequently. Errors are common, even for experienced programmers. 80 lines is probably dangerous, since errors may lurk in the code and be hard to find and correct.

If the programmer does NOT find an error on line 7, the programmer should check line _____.

6 Sometimes a compiler doesn't notice an error exists until getting confused on a later line. Thus, the programmer should check the previous line, and if an error isn't found there, check even earlier lines.

A compiler generates the following error messages: Line 7: Missing semicolonLine 9: numItems not defined Line 10: Expected '(' The programmer should start by examining line _____.

7 The first error message refers to line 7. The programmer should focus on the first error message.


Conjuntos de estudio relacionados

International Business Chapter 9

View Set

Pathophysiology Module 18 Musculoskeletal Disorders

View Set

MUL1010 Chapter 9 SmartBook Baroque Solo and Chamber Music

View Set

Pediatrics: Chapter 24: Genitourinary Disorders

View Set

Porth's PrepU: Chapter 32- Disorders of Cardiac Function

View Set

BUS 137 Principles of Management My Maria Rivera

View Set