1 Web Based Programming Section 6 James King 12 August 2003.

Slides:



Advertisements
Similar presentations
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
CS102--Object Oriented Programming
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Index Exception handling Exception In Java Exception Types
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Guide To UNIX Using Linux Third Edition
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Javascript and the Web Whys and Hows of Javascript.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
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.
Object Oriented Programming with Java (150704).  Throwable Exception (This class will catch exceptions generated by prog.) (Create your own custom exception.
Java: Chapter 1 Computer Systems Computer Programming II.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
BİL528 – Bilgisayar Programlama II Making Decisions, Loops, Debugging, Designing Objects Using Classes 1.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Classes CS 21a: Introduction to Computing I First Semester,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction to Exception Handling and Defensive Programming.
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.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Documentation and Programming Style Appendix A © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Documentation javadoc. Documentation not a programmer's first love lives in a separate file somewhere usually a deliverable on the schedule often not.
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
CSC 212 – Data Structures Prof. Matthew Hertz WTC 207D /
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
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.
Information and Computer Sciences University of Hawaii, Manoa
MIT AITI 2003 Lecture14 Exceptions
Testing and Debugging.
CS102 – Exceptions David Davenport Latest: May 2015
Chapter 1: Computer Systems
Classes CS 21a: Introduction to Computing I
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.
CMSC 202 Exceptions.
Corresponds with Chapter 5
Presentation transcript:

1 Web Based Programming Section 6 James King 12 August 2003

2 Key Skills This section will teach how to: make your programs readable document you programs debug your programs

3 Making code Readable The key to readability is indentation it is difficult to see which bit of code is inside which if or loop if your presentation is messy If you miss out a { or } it can be difficult to find the correct place to add one

4 How not to Indent public void nestit() { for (int i=0; i<3; i=i+1){ System.out.println("top of outer loop i "+i); for (int j=0; j<3; j=j+1) { System.out.println("inside inner loop i "+i+" j "+j); } System.out.println("bottom of outer loop i "+i); } There is a } missing somewhere

5 One Way to indent (Pascal Method) Each time you type a { all lines following are indented more Each time you type a } all lines following are indented less both { and } are on separate lines by themselves it is easy to match up { and } pairs like this...

6 One way to Indent (Pascal Method) public void nestit() { for (int i=0; i<3; i=i+1) { System.out.println("top of outer loop i "+i); for (int j=0; j<3; j=j+1) { System.out.println("inside inner loop i "+i+" j "+j); } System.out.println("bottom of outer loop i "+i); } There is a } missing !!

7 Indentation Style We don’t mind which way you indent as long as you are consistent it is easy to see nesting it is easy to see where the blocks start and end

8 Comments and automatic documentation Generation Key skills section

9 Documentation - Comments The simplest form of documentation for you code are comments. This allows you to add English descriptions to your code These can be placed almost anywhere in your code Comments can extend over multiple lines and are enclosed inside /* comment */ Single line comments can be made with // comment

10 Comments Example /* simple demonstration of the how the continue statement works inside a loop */ public void loopit() // takes no parameters { System.out.println("before loop"); for (int i=0; i<5; i=i+1) { System.out.println(" before if"+i); if (i==3) continue; System.out.println(" after if"+i); // bypassed if i is 3 // } oops I commented out the end of block marker by accident... System.out.println("after loop"); }

11 Automatic Documentation Generation Java compiler can generate HTML web pages from your comments if you place them in certain places in a certain style comments go before the declarations /** * about this procedure **/ public void proc() comments in the body of blocks are ignored

12 Documentation Tags In addition to describing the class inside the comment you can add a version and authors name using name You can document the parameters and return valves of a variable description There are more tags...

13 Documentation Example /** * description of the class (your name) (a version number or a date) */ public class doc { // description of the attribute private int x; /** * description of the constructor */ public doc() {} /** * description of the method y description of the parameter description of the return value */ public int sampleMethod(int y) {return x + y; } }

14 HTML Documentation

15 Debugging Key skills section

16 Debugging Code Most compilers come with a debugger which usually allows you to Step though your program line by line Examine the value of each attribute and variable in scope Run the code at full speed and stop at certain points (breakpoint) In addition BlueJ allows you to Call a method and optionally provide values for the parameters it requires Create a new instance of a class

17 Debuggers are useful in several situations You want to understand how someone else's code works and it is too complex to run in your head (reverse engineer) You want to find out why your program is not doing what you expect it to and make it work correctly (debug) You want to see how Java runs your programs and how the ifs and loops work

18 Debugging Debugging is a practical activity so you will learn how to do this in the practical sessions.

19 Catching and Generating Runtime Errors Advanced Java Facilities

20 Dealing with problems in a running program The compiler tries to catch errors during byte code creation. However some errors only occur when the program is running int i=1; i=i-1; int b=3/i; 3 divided by 0 is infinity. Infinity can not be stored in a int The Java interpreter generates a ArithmeticException and stops running the program

21 Catching Exceptions To avoid the program stopping we can catch the exception try { int i=1; i=i-1; int b=3/i; } catch (Exception e) { System.err.println(“CRASH”); } Any exception generated anywhere in this block of code will cause execution to jump to the catch block, skipping any remaining code. If there is no exception the catch block is skipped In either case execution eventually gets here

22 Information about the Exception The e in catch (Exception e) acts just like a parameter and is actually an object. It contains information about the exception e.printStackTrace(); displays on the console screen the class and line number the exception was generated at e.getMessage(); returns a string with details of why the exception occurred

23 Catching different types of exceptions try { } Catch (ArithmeticException f) { } catch (Exception e) { } This will catch only ArithmeticException and any subclasses Since all Exceptions are subclasses of Exception this will pick up all other exceptions

24 Cleaning up the mess Sometimes regardless of if an exception is generated or not you want some code to be executed try { } catch (Exception e) { } finally { }

25 Exceptions Java built in methods and classes may generate exceptions if used incorrectly The Java compiler expects you to either catch these possible exceptions in the method they could be created in or leave it to the method that called that method to handle them…

26 Throws Java expects each method to catch any exceptions it could generate If you don’t want to do this you must add the uncaught exceptions to the method definition in a throws list void problem2() throws Exception { throw new Exception(); }