Error messagesTypes of errorsWe’re only humanWhat to do about errorsSyntax errorsExample syntax errorsRuntime errorsA common runtime errorNull pointer exceptionsAssertion errorsLogic errorsMake better use of your timeApproaches to finding errorsGood approaches, IGood approaches, IIA “best” approachTesting is your responsibilityWhy such high standards?The End (for now)Jan 13, 2019Error messages2Types of errorsThere are three general types of errors:Syntax (or “compile time”) errorsSyntax errors are “grammatical” errors and are detected when you compile the programSyntax errors prevent your program from executingRuntime errorsRuntime errors occur when you tell the computer to do something illegalRuntime errors may halt execution of your programLogic errorsLogic errors are not detected by the computerLogic errors cause your results to be wrong3We’re only humanFor the novice programmers in the room (experienced programmers already know this):Your programs will be full of errors, of all kindsYou cannot avoid this, no matter how good you getYou cannot learn not to make errors--it’s impossibleBut, with practice, you will get a little bit betterYou can and must learn:How to recognize the various types of errorsHow to find and fix themWhat is important is not avoiding errors, but knowing how to fix them4What to do about errorsError messages are your friends--read them and try to understand themWith practice, you can fix most syntax errors almost immediatelyRuntime and logic errors may take considerably longer to track down and fixHere’s what’s important to remember:Everyone makes lots of stupid errors (and almost all errors are stupid ones--mine included); it’s nothing to be ashamed ofHowever, it is not OK to let those errors surviveApproximately 90% of your time will be spent debugging5Syntax errorsA syntax error is a “grammatical” error--bad punctuation, misuse of keywords, etc.Syntax error messages tell you two things:The line number at which an error was detectedUsually, this is the line containing the errorIn some cases, the actual error is earlier in the program textUse an editor that shows line numbers!What the compiler thinks the problem isSince the compiler cannot know what you meant, the message is only a “best guess,” and is sometimes misleadingSyntax errors can cascade: An early error can cause spurious error messages later in the programAlways fix the earliest message firstIf later messages don’t make sense, try recompiling6Example syntax errorsSystem.out.println("(g1 == g2) = " + (g1 == g2);')' expectedSystem.out.println("(g1 == g2) = " + (g1 == g2)));’;' expecteda = g1 + g2;cannot resolve symbol -- variable aa was never declared; ora was declared, but not where it can be seen from herea = 5;<identifier> expectedThis is a statement, and statements can only occur inside methodsa = b;variable b might not have been initialized7Runtime errorsA runtime error occurs when your program does something illegalRuntime errors typically stop your programRuntime errors can be “caught” by your program, thus allowing the program to continue runningWe’ll discuss how to do this later in the courseRuntime errors are usually caused by something the program did (or failed to do) earlier in executionBecause the cause of the error is somewhere else in the program, runtime errors are usually harder to solve8A common runtime errornjava.lang.NullPointerException at Test.run(Test.java:22) at Test.main(Test.java:6) at __SHELL1.run(__SHELL1.java:6) at bluej.runtime.ExecServer.suspendExecution(ExecServer.java:187) at bluej.runtime.ExecServer.main(ExecServer.java:69)} What kind of error it wasTraceback: How your program got to where the error was detected (in line 22 of the file Test.java)The part of the traceback in your code (line 22 of the file, in your run method, called from line 6 of the file, in your main method)The part of the traceback in the Java and BlueJ system; you can pretty much ignore this part9Null pointer exceptionsThe NullPointerException is one of the most common runtime errorsIt occurs when you send a message to a null variable (anon-primitive variable that doesn’t refer to an actual object)The null variable causing the problem is always just before a dotExample: g.drawLine(x1, y1, x2, y2);If this caused a NullPointerException, the variable g must have been nullYou probably never initialized gJava tries to catch uninitialized variables, but it cannot catch them all10Assertion errorsassert 2 + 2 == 5;Exception in thread "main" java.lang.AssertionError at Test.run(Test.java:9) at Test.main(Test.java:6)assert 2 + 2 = 5: "2 + 2 is actually " + (2 + 2);Exception in thread "main" java.lang.AssertionError:2 + 2 is actually 4 at Test.run(Test.java:9) at Test.main(Test.java:6)Use assert statements to tell what you believe will always be true at a given point in the programassert statements are best used as an “early warning system,” rather than as a debugging tool once errors are known to happenassert statements are also valuable as documentation11Logic errorsA logic error is when your program compiles and runs just fine, but does the wrong thingIn very simple programs, logic errors are usually easy to find and fix, if they occur at allIn all but the simplest of programs,10% of your time is spent writing the program and fixing the syntax errors (more if you are still learning the syntax)90% of your time is spent finding and fixing runtime and logic errorsLogic errors can take hours, or even days, to findAllocate your time accordingly!12Make better use of your timeWhile you cannot avoid making errors, you can prepare for themKeep your programs as simple as possibleIndent properly so you can see the structure of your codeComment your programs so you can find things againWrite test cases and use them“Write a little, test a little”Write assert statements for the things that you “know” cannot possibly happenProgramming is a creative activity, and can be very enjoyable and satisfyingFor most of us, debugging is not the fun part13Approaches to finding errorsTry a random change and see if it helps“An infinite number of monkeys, given an infinite number of
View Full Document