Error messages 25-Apr-17.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Coding and Debugging. Requirements and Specification Recall the four steps of problem solving: Orient, Plan, Execute, Test Before you start the implementation.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Debugging Introduction to Computing Science and Programming I.
Python Programming Chapter 1: The way of the program Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program.
How to Debug VB .NET Code.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
Introduction to a Programming Environment
C++ Crash Course Class 1 What is programming?. What’s this course about? Goal: Be able to design, write and run simple programs in C++ on a UNIX machine.
Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Chapter 12: Exception Handling
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Slides Credit Umair Javed LUMS Web Application Development.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
The Java Programming Language
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
2_Testing Testing techniques. Testing Crucial stage in the software development process. Significant portion of your time on testing for each programming.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
1 Computer Applications Java Structure Terminology Common Compiler Errors.
DEBUGGING. BUG A software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
CSC 212 – Data Structures Prof. Matthew Hertz WTC 207D /
C++ crash course Class 9 flight times program, using gdb.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
Programming Errors. Errors of different types Syntax errors – easiest to fix, found by compiler or interpreter Semantic errors – logic errors, found by.
Exceptions and Assertions Chapter 15 – CSCI 1302.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Error Handling Tonga Institute of Higher Education.
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Introduction to Computing Science and Programming I
Testing and Debugging.
Debugging CMSC 202.
Exceptions 10-Nov-18.
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Chapter 15 Debugging.
Exceptions 19-Feb-19.
Error messages 24-Feb-19.
Error messages 24-Feb-19.
Exceptions 7-Apr-19.
Tonga Institute of Higher Education IT 141: Information Systems
An Introduction to Debugging
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Error messages 14-Apr-19.
Error messages 16-Apr-19.
Tonga Institute of Higher Education IT 141: Information Systems
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Error messages 4-May-19.
Exceptions 10-May-19.
Error messages 3-Jun-19.
Exceptions 5-Jul-19.
Presentation transcript:

Error messages 25-Apr-17

Types 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 wrong

Syntax 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 recompiling

Example syntax errors a = g1 + g2; a = 5; a = b; 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 in some other scope; it’s not visible here a = 5; <identifier> expected This is a statement, and statements can only occur inside methods a = b; variable b might not have been initialized

Runtime 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 solve

A common runtime error } What kind of error it was java.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 was Traceback: 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 Eclipse system; you can pretty much ignore this part

Null pointer exceptions The NullPointerException is one of the most common runtime errors It occurs when you send a message to a null variable (a non-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 all

Assertion 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 documentation

Logic 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!

Make 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 part

Approaches to finding errors Try a random change and see if it helps “An infinite number of monkeys, given an infinite number of typewriters, will eventually produce the complete works of Shakespeare” No monkey has ever passed this course You can’t afford this approach--it’s stupid and doesn’t work Better approach: Think about what could have caused the results you see, and then look for where that might have occurred Advantage: Leads you directly to the error Disadvantage: Not always possible to figure out what could have happened

Good approaches, I Put in print statements Use a Debugger Advantage: Helps you see what the program is doing Disadvantage: You have to take them out again Use a Debugger A debugger is a program that lets you step through your program line by line and see exactly what it is doing Advantages: Takes no prior preparation Lets you see exactly what the program is doing Disadvantage You have to learn to use one (but it’s worth the trouble!)

Good approaches, II Explain your code to a friend (even if your friend can’t program) Advantage: Forces you to actually look at what you did Disadvantage: Requires you to have friends In a pinch, even your dog will do (if you can get him to hold still long enough) Note: This is not a violation of academic honesty! In extremis: Throw out the offending code and rewrite it Advantages: Usually works Usually makes your code simpler Disadvantage: Can be psychologically difficult

A “best” approach According to the Extreme Programming (XP) philosophy, you should write a suite of tests before you write the code This helps clarify what the code is supposed to do It’s a good idea to know this before starting to program! Having a test suite also makes it much less risky to change and improve working code The mantra is: “Write a little, test a little.” That is, make changes in small steps, and ensure after each step that the code still works This only makes sense if running the tests is fast and simple Use JUnit to build and run your test suites

Testing is your responsibility You are expected to turn in correct programs, or as nearly correct as you can make them Testing is a vital part of writing programs Test your program, not only with “typical” data, but also all the weird and unusual cases that you can think of When I supply example data, it is only to help explain what your program should do; you should never think of such data as an adequate test set We will usually test your program with data you have never seen To get 100%, your programs must pass every test and have good style Programs with syntax errors are worth zero points (You’ve done less than 10% of the work)

Why such high standards? Large programs are the most complex entities ever devised by mankind Large programs consist of thousands of methods and hundreds of thousands of lines A single error can cause catastrophic failure There are a great many examples, from space vehicles crashing to deaths from badly programmed medical equipment We give partial credit for “mostly working” programs--and, believe it or not, I think this is generous

The End (for now) “To err is human, but to really foul things up requires a computer.” Farmer’s Almanac, 1978