DOC PREVIEW
Penn CIT 591 - Error messages

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 errorsThere are three general types of errors:Syntax (or “compile time”) errorsSyntax errors are “grammatical” errors and are detected when you compile the programSyntax errors prevent your program from executingRuntime errorsRuntime errors occur when you tell the computer to do something illegalRuntime errors may halt execution of your programLogic errorsLogic errors are not detected by the computerLogic errors cause your results to be wrong3We’re only humanFor the novice programmers in the room (experienced programmers already know this):Your programs will be full of errors, of all kindsYou cannot avoid this, no matter how good you getYou cannot learn not to make errors--it’s impossibleBut, with practice, you will get a little bit betterYou can and must learn:How to recognize the various types of errorsHow to find and fix themWhat is important is not avoiding errors, but knowing how to fix them4What to do about errorsError messages are your friends--read them and try to understand themWith practice, you can fix most syntax errors almost immediatelyRuntime and logic errors may take considerably longer to track down and fixHere’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 ofHowever, it is not OK to let those errors surviveApproximately 90% of your time will be spent debugging5Syntax errorsA 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 detectedUsually, this is the line containing the errorIn some cases, the actual error is earlier in the program textUse an editor that shows line numbers!What the compiler thinks the problem isSince the compiler cannot know what you meant, the message is only a “best guess,” and is sometimes misleadingSyntax errors can cascade: An early error can cause spurious error messages later in the programAlways fix the earliest message firstIf later messages don’t make sense, try recompiling6Example syntax errorsSystem.out.println("(g1 == g2) = " + (g1 == g2);')' expectedSystem.out.println("(g1 == g2) = " + (g1 == g2)));’;' expecteda = g1 + g2;cannot resolve symbol -- variable aa was never declared; ora was declared, but not where it can be seen from herea = 5;<identifier> expectedThis is a statement, and statements can only occur inside methodsa = b;variable b might not have been initialized7Runtime errorsA runtime error occurs when your program does something illegalRuntime errors typically stop your programRuntime errors can be “caught” by your program, thus allowing the program to continue runningWe’ll discuss how to do this later in the courseRuntime errors are usually caused by something the program did (or failed to do) earlier in executionBecause 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 exceptionsThe NullPointerException is one of the most common runtime errorsIt 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 dotExample: g.drawLine(x1, y1, x2, y2);If this caused a NullPointerException, the variable g must have been nullYou probably never initialized gJava tries to catch uninitialized variables, but it cannot catch them all10Assertion errorsassert 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 programassert statements are best used as an “early warning system,” rather than as a debugging tool once errors are known to happenassert statements are also valuable as documentation11Logic errorsA logic error is when your program compiles and runs just fine, but does the wrong thingIn very simple programs, logic errors are usually easy to find and fix, if they occur at allIn 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 errorsLogic errors can take hours, or even days, to findAllocate your time accordingly!12Make better use of your timeWhile you cannot avoid making errors, you can prepare for themKeep your programs as simple as possibleIndent properly so you can see the structure of your codeComment your programs so you can find things againWrite test cases and use them“Write a little, test a little”Write assert statements for the things that you “know” cannot possibly happenProgramming is a creative activity, and can be very enjoyable and satisfyingFor most of us, debugging is not the fun part13Approaches to finding errorsTry a random change and see if it helps“An infinite number of monkeys, given an infinite number of


View Full Document

Penn CIT 591 - Error messages

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Error messages
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Error messages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Error messages 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?