Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
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.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
ITEC200 Week02 Program Correctness and Efficiency.
Program Correctness and Efficiency Chapter 2. Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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:
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
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.
Program Correctness and Efficiency Chapter 2. Chapter Objectives  To understand the differences between the three categories of program errors  To understand.
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.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives Intro - Software OOP Inheritance, interfaces,
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Program Correctness and Efficiency Chapter 2 Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
Fall 2007CS 225 Program Correctness and Efficiency Chapter 2.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
Java Software Solutions Foundations of Program Design Sixth Edition
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
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:
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Exceptions in the Java programming language J. W. Rider.
Exception Handling Chapter 9.
Java Programming Language
Chapter 12 Exception Handling
Web Design & Development Lecture 7
Program Correctness and Efficiency
Exception Handling.
Presentation transcript:

Programming & Debugging

Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Modularity Why? –Smaller pieces are easier to understand –Easier to isolate proper piece for debugging, modification –Good modules are easier to reuse How? –Abstraction –Obiect-oriented Design

Modifiability Why? –Programs are never static; problems change –Avoid reinventing the wheel as much as possible How? –Good structure (classes & functions) –Named constants –Typedef (e.g. typedef float RealType;) –Good documentation!

Ease of Use Why? –So it will be used How? –Clear prompts for input –Echo the input –Clear output –Adapt program to user, not vice versa –See CPSC 222 for more!

Fail-Safe Programming Why? –Input isn’t always what it should be (from users or otherwise) How? –Validate input data (ensure preconditions satisfied) –Use assert to check preconditions and invariants (assertions)

Style Why? –Good style addresses all the other issues! –Programs are also read by people How? –Style guides vary –Pick one (the textbook guidelines are good) and be consistent! –Many of the “style” guidelines in the book would come under good modularity in my mind.

Issues of Style Reasonable-sized methods Private data members (inspectors/accessors & mutators as necessary) Avoid global variables Use void methods for side effects Readability & Documentation

Good (consistent!) use of indentation Liberal use of blank space Useful identifier names Identify author/date on each file (use JavaDoc template) Comment per class (what it provides) Comment for any non-obvious data item (class member or local variable) Comment per method (pre and post conditions, what it does, explain parameters) Comment at important / tricky steps in function (e.g. invariants)

Debugging Why? –Because there are bugs How? –Compiler’s help (for syntax errors) –Output statements –IDE Debugger Break Watch Step

Program Defects and “Bugs” A program may be efficient, but is worthless if it produces a wrong answer Defects often appear in software after it is delivered Testing can never demonstrate the complete absence of defects In some situations it is very difficult to test a software product completely in the environment in which it is used Debugging: removing defects

Types of Errors Syntax Error Runtime Error (or Exception) Logic Error

Syntax Errors Mistakes in grammar of the language Detected by compiler; prevent successful compilation Examples: –Omitting or misplacing braces or semicolons –Performing an incorrect type of operation on a primitive type value –Invoking an instance method not defined –Not declaring a variable before using it –Providing multiple declarations of a variable

Run-time Errors or Exceptions Run-time errors –Occur during program execution –Occur when the JVM detects an operation that it knows to be incorrect –Cause the JVM to throw an exception Examples of run-time errors include –Integer diivision by zero –Array index out of bounds –Number format and Input mismatch error –Null pointer exceptions

Java Exceptions

Logic Errors The program runs without error, but doesn’t do what was expected (in at least one case) Examples: –Calculates the wrong answer –Does not halt –Ignores valid inputs or data

Logic Errors A logic error occurs when the programmer or analyst –Made a mistake in the design of a class or method –Implemented an algorithm incorrectly Most logic errors do not cause syntax or run- time errors and are thus difficult to find Sometimes found through testing Sometimes found during real-world operation of the program

The Exception Class Hierarchy When an exception is thrown, one of the Java exception classes is instantiated Exceptions are defined within a class hierarchy that has the class Throwable as its superclass Classes Error and Exception are subclasses of Throwable RuntimeException is a subclass of Exception

Throwable Exception Hierarchy

Methods of Throwable (Inherited by subclasses)

Checked and Unchecked Exceptions Checked exception –Beyond control of programmer –Subclass of Exception (but not RuntimeException) Unchecked exception may result from –Programmer error –Serious external conditions that are unrecoverable –Subclasses of RuntimeException

Example: Checked Exceptions

Exception Hierarchy Unchecked exceptions

Handling Exceptions Unchecked exceptions (including errors) –These are considered unrecoverable –Programmers not “expected” to handle them (but expected not to cause them) Checked exceptions –Due to external conditions, often recoverable

Catching and Handling Exceptions When an exception is thrown, the normal sequence of execution is interrupted Default behavior –Program stops –JVM displays an error message and stack trace The programmer may override the default behavior by –Enclosing statements in a try block –Processing the exception in a catch block

The try-catch-finally Sequence Avoid uncaught exceptions –Write a try-catch sequence to catch an exception –Handle it rather than relying on the JVM Catch block is skipped if all statements within the try block execute without error Finally (if provided) is executed after try block or catch block –Use this to “clean up” (e.g. close open files)

Try-catch-finally try { //statements that might cause exception } catch (EOFException ex){ //code for EOF Exception } catch(IOException ex){ //code for IO Exception } finally{ //code executed after try or catch code }

Handling Exceptions to Recover from Errors Exceptions provide the opportunity to –Recover from errors (preferable, if possible) –Report errors First matching Catch block (only) is executed –Match according to the type of exception Compiler displays an error message if it encounters an unreachable catch clause –Example: IOException with no IO in try clause

Throwing Exceptions If method doesn’t catch exception, it can throw it (to its caller) –Add a throws clause to the method header –Explicitly throw the exception, using a throw statement The throws clause is useful if a higher- level module already contains a catch clause for this exception type

Throw statement Can use a throw statement in a lower- level method to indicate that an error condition has been detected Once the throw statement executes, the lower-level method stops executing immediately Throw statement useful for user-defined exceptions (e.g. item not found)

Catching Exceptions Example

Guidelines for Exceptions If an exception is recoverable in the current method, handle the exception in the current method If a checked exception is likely to be caught in a higher-level method, declare that it can occur using a throws clause It is not necessary to use a throws clause with unchecked exceptions