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.

Slides:



Advertisements
Similar presentations
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Advertisements

Yoshi
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
For use of Cleveland State's IST410 Students only 1 Exception.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
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 in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Debugging CMSC 201. Announcements Hw2, Hw3 grades returned if something doesn’t seem right, ask Midterm next Thurs (Oct 23) we’ll review on Tues no hw.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CS203 Java Object Oriented Programming Errors and Exception Handling.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Chapter 12: Exception Handling
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
JSP Exception Handling 20-Oct-15. JSP - E XCEPTION H ANDLING When you are writing JSP code, a programmer may leave a coding errors which can occur at.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Practice Session 9 Exchanger CyclicBarrier Exceptions.
FT228/3 Web Development Error processing. Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Exceptions In this lecture:
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
Testing and Debugging.
Introduction to Exceptions in Java
Week 14 - Wednesday CS 121.
Creating and Modifying Text part 2
Exceptions 10-Nov-18.
Chapter 12 Exception Handling
Web Design & Development Lecture 7
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Exceptions 10-May-19.
Java Basics Exception Handling.
Presentation transcript:

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 –more difficult to fix, but at least you're using java and you get these  Your program just doesn't do the right thing.

Nov 10, Fall 2006IAT 8003 Compiler Errors  Errors dealing with language syntax,  Simple logical errors, –Whatever the compiler can possibly catch.  Generally, the line number stated has the error on it. –Sometimes the fix is elsewhere

Nov 10, Fall 2006IAT 8004 How to fix compiler errors?  Start at the top of the error list  Some errors cause others –Wrong variable declaration causes errors in usage of that variable  Use the line number!  If that line looks OK, check the line above –maybe missed a brace/semicolon or other necessary syntax element.

Nov 10, Fall 2006IAT 8005 Compile Time Errors  Some errors aren't necessarily errors. –For example: String foo; //assume we initialize this somewhere else public void blah(){ Object bar; try{ bar = foo.toString(); } catch(Exception e){ println(“Oh no!!”); return; } println(bar.toString()); //lets call this line 101 } –Will give you something like: line 101: variable bar might not be initialized! (or something like that)

Nov 10, Fall 2006IAT 8006 Runtime Exceptions  There are two types of Runtime Exceptions –Checked and Unchecked  Checked exceptions: –Java makes you deal with these in your code –Things that you would expect to fail: I/O mainly  Unchecked exceptions –Java does not require you to catch these

Nov 10, Fall 2006IAT 8007 Checked Exceptions  IOException (FileNotFoundException)  Input and output is typically hare to write because you have to deal with the real world’s complexities  Java requires that you put these in Try/Catch Blocks –Processing manages some of this

Nov 10, Fall 2006IAT 8008 Unchecked Exceptions  Exceptions that only the programmer can anticipate –Extremely hard for a compiler to determine  NullPointerException (NPE) and ArrayIndexOutOfBoundsException (AIOBE)  Caused by semantic errors –uninitialized variable, bad loop logic…

Nov 10, Fall 2006IAT 8009 Exceptions  On exception, you get a stack trace  Find the first line of the stack trace that occurs in your program.  That line is where the exception occurred, not necessarily where the fix is. –On that line, did you get an NPE? –Is there some object that you're calling a method on? Is that object Null? –For AIOBE, check index values

Nov 10, Fall 2006IAT print your variables  System.out.println() (or println() in p5) –Use println often –Print everything: array values, pointer values, array index, objects etc –Each println should label itself with class name and line number –Be sure to use System.out.flush(); to ensure you are getting all data

Nov 10, Fall 2006IAT Learn to read your code  Keep a notepad around to keep track of variable values. –Use commenst to document complex code –Keep one step to one line. –Format your code! Indentations help readability

Nov 10, Fall 2006IAT Things to remember  In java Objects are passed by reference and primitives are passed by value. public void doStuff(String a) { a = a + “bar”; } public void doMoreStuff(int a) { a = a+5; } public static void main(...){ String temp = “foo”; int temp2 = 5; doStuff(temp); doMoreStuff(temp2); System.out.println (temp); System.out.println (temp2); } prints out: foobar 5

Nov 10, Fall 2006IAT The #1 debugging tip  TEST YOUR CODE OFTEN! –Catching your small errors early will help you avoid the big complicated errors later. –If you write a chunk of code that you can test, test it. –You'll regret not spending 5 minutes writing a simple test case when you spend hours trying to find out it has a bug later.