Software Construction



Similar documents
CS 111 Classes I 1. Software Organization View to this point:

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

Java Application Developer Certificate Program Competencies

Lecture J - Exceptions

Software Engineering Techniques

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Java Programming Language

Computing Concepts with Java Essentials

Introduction to Programming System Design. CSCI 455x (4 Units)

Exception Handling In Web Development DevelopIntelligence LLC

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

D06 PROGRAMMING with JAVA

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Designing and Writing a Program DESIGNING, CODING, AND DOCUMENTING. The Design-Code-Debug Cycle. Divide and Conquer! Documentation is Code

10 Java API, Exceptions, and Collections

Fundamentals of Java Programming

No no-argument constructor. No default constructor found

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Glossary of Object Oriented Terms

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Evaluation of AgitarOne

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Object Oriented Software Design II

public static void main(string args[]) { System.out.println( "f(0)=" + f(0));

Java CPD (I) Frans Coenen Department of Computer Science

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

AP Computer Science Java Subset

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Lab work Software Testing

Java SE 8 Programming

Testing, Debugging, and Verification

Computer Programming I

Java (12 Weeks) Introduction to Java Programming Language

Official Android Coding Style Conventions

Programming by Contract vs. Defensive Programming: A Comparison of Run-time Performance and Complexity

Java Interview Questions and Answers

LINKED DATA STRUCTURES

PL/SQL Practicum #2: Assertions, Exceptions and Module Stability

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Announcement. SOFT1902 Software Development Tools. Today s Lecture. Version Control. Multiple iterations. What is Version Control

Documentum Developer Program

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

TypeScript for C# developers. Making JavaScript manageable

Rigorous Software Development CSCI-GA

RMI Client Application Programming Interface

Adapting C++ Exception Handling to an Extended COM Exception Model

Crash Course in Java

Java from a C perspective. Plan

UI Performance Monitoring

How To Write A Test Engine For A Microsoft Microsoft Web Browser (Php) For A Web Browser For A Non-Procedural Reason)

Android Application Development Course Program

#820 Computer Programming 1A

Lecture 1 Introduction to Android

Chapter 3: Restricted Structures Page 1

Java EE Web Development Course Program

Instrumentation Software Profiling

The Darwin Game 2.0 Programming Guide

Operating System Structures

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Logging in Java Applications

Approach of Unit testing with the help of JUnit

Syllabus for CS 134 Java Programming

Using the Monitoring and Report Viewer Web Services

Introduction to Java and Eclipse

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

A Practical Guide to Test Case Types in Java

Design by Contract beyond class modelling

Lecture 5: Java Fundamentals III

Specialized Programme on Web Application Development using Open Source Tools

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

JAAS Java Authentication and Authorization Services

COMP 110 Prasun Dewan 1

Java Troubleshooting and Performance

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

vector vec double # in # cl in ude <s ude tdexcept> tdexcept> // std::ou std t_of ::ou _range t_of class class V Vector { ector {

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

15-214: Principles of Software Construction 8 th March 2012

CIS 544 Advanced Software Design and Development. Project Management System. Oreoluwa Alebiosu

CS3600 SYSTEMS AND NETWORKS

bbc Developing Service Providers Adobe Flash Media Rights Management Server November 2008 Version 1.5

Monitoring Java enviroment / applications

Interface Definition. Guidelines and Recommendations. Author: Radovan Semančík. Date: January Version: 1.0

EVALUATING METRICS AT CLASS AND METHOD LEVEL FOR JAVA PROGRAMS USING KNOWLEDGE BASED SYSTEMS

Object Relational Database Mapping. Alex Boughton Spring 2011

Restraining Execution Environments

Getting Started with the Internet Communications Engine

Transcription:

Software Construction Debugging and Exceptions Jürg Luthiger University of Applied Sciences Northwestern Switzerland Institute for Mobile and Distributed Systems Learning Target You know the proper usage of exceptions know how to use the assert keyword can use the Eclipse Debugger Institut für Mobile und Verteilte Systeme J. Luthiger 2

Agenda Introduction into Exceptions Introduction into Assert Using the Eclipse Debugger Institut für Mobile und Verteilte Systeme J. Luthiger 3 The nature of Exceptions Exceptions due to programming errors e.g. NullPointerException, IllegalArgumentException due to client code errors wrong usage of the API due to resource failures out of memory network connection failure Institut für Mobile und Verteilte Systeme J. Luthiger 4

Pitfalls If not used correctly, exceptions can slow down the program, as it takes CPU power to create, throw and catch exceptions If overused, exceptions make the code difficult to read and are frustrating for programmers to use the API Institut für Mobile und Verteilte Systeme J. Luthiger 5 The Problem using Exceptions public void consumeandforgetallexceptions() { try { some code that throws exceptions catch (Exception e) { ex.printstacktrace(); Execution of the program continues after the catch block, as if nothing had happened public void somemethod() throws Exception { How can a blank method throw an exception? Institut für Mobile und Verteilte Systeme J. Luthiger 6

Compiler Error vs. Runtime Errors Compiler errors are generated by the compiler. They are developer friendly! Runtime errors are generated by the runtime system-> Program crash if exceptions are not handled correctly, not at all user friendly The handling of the exceptions is developerintensive Institut für Mobile und Verteilte Systeme J. Luthiger 7 Exception Types in Java java.lang.runtimeexception => UNCHECKED no compiler checks java.lang.exception => CHECKED Compiler checks, if there exist an Exception Handling Institut für Mobile und Verteilte Systeme J. Luthiger 8

Sample Exception Hierarchy Exception RuntimeException SQLException NullPointerException Institut für Mobile und Verteilte Systeme J. Luthiger 9 Misuse of Checked Exceptions Checked Exceptions are a forced contract on the invoking layer to catch or to throw it. Unwanted burden of the client code is unable to deal with the exception effectively using empty catch block just throwing it and placing burden on the invoker Breaking encapsulation public List getallaccounts() throws FileNotFoundException, SQLException { Institut für Mobile und Verteilte Systeme J. Luthiger 10

Best Practices: 1 When deciding on checked exceptions vs. unchecked exceptions, ask yourself, "What action can the client code take when the exception occurs?" If the client can take alternate action to recover from the exception, make it a checked exception. If the client cannot do anything useful, then make the exception unchecked. Institut für Mobile und Verteilte Systeme J. Luthiger 11 Best Practices: 2 Preserve encapsulation For example, do not propagate SQLException from the database to the application layer. Convert them into unchecked exceptions: try {...some code that throws SQLException catch (SQLException e) { throw new RuntimeException(ex); Institut für Mobile und Verteilte Systeme J. Luthiger 12

Best Practices: 3 Try not to create new custom exceptions if they do not have useful information for client code. public class DuplicateUsernameException extends Exception { should be better public class DuplicateUsernameException extends Exception { public DuplicateUsernameException (String username){... public String requestedusername(){... public String[] availablenames(){... or even simpler throw new RuntimeException("Username already taken"); Institut für Mobile und Verteilte Systeme J. Luthiger 13 Best Practices: 4 Document Exceptions Use Javadoc @throws tag Write Unit test @Test(expected = IndexOutOfBoundsException.class) public void testindexoutofboundsexception() { ArrayList blanklist = new ArrayList(); blanklist.get(10); Institut für Mobile und Verteilte Systeme J. Luthiger 14

Using Exceptions: 1 Always clean up after yourself Resources like database or network connections should always be closed public void dataaccesscode(){ Connection conn = null; try { conn = getconnection();..some code that throws SQLException catch(sqlexception ex){ ex.printstacktrace(); finally{ DBUtil.closeConnection(conn); Institut für Mobile und Verteilte Systeme J. Luthiger 15 Using Exceptions: 2 Never use exceptions for flow control public void useexceptionsforflowcontrol() { try { while (true) { increasecount(); catch (MaximumCountReachedException ex) { //Continue execution public void increasecount() throws MaximumCountReachedException { if (count >= 5000) throw new MaximumCountReachedException(); Institut für Mobile und Verteilte Systeme J. Luthiger 16

Using Exceptions: 3 Do not suppress or ignore exceptions When a method from an API throws a checked exception, it is trying to tell you that you should take some counter action. If the checked exception does not make sense to you, do not hesitate to convert it into an unchecked exception and throw it again. Institut für Mobile und Verteilte Systeme J. Luthiger 17 Using Exceptions: 4 Do not catch top-level exceptions Unchecked exceptions inherit from the RuntimeException class, which in turn inherits from Exception. By catching the Exception class, you are also catching RuntimeException as in the following code: try{.. catch(exception ex) { Institut für Mobile und Verteilte Systeme J. Luthiger 18

Using Exceptions: 5 Log exceptions just once Logging the same exception stack trace more than once can confuse the programmer examining the stack trace about the original source of exception. So just log it once. Institut für Mobile und Verteilte Systeme J. Luthiger 19 Summary Guidelines 1. The caller MUST handle the exception => CHECKED 2. A Minority will handle the exception => UNCHECKED 3. Is a runtime error irresolvable => UNCHECKED 4. Still unclear? => UNCHECKED Institut für Mobile und Verteilte Systeme J. Luthiger 20

Exception chaining Exception chaining means that the constructor of an exception object takes a nested or cause exception as an argument. The newly created higher level exception object will then maintain a reference to the underlying root cause exception.... catch (FileNotFoundException e) { throw new MyException("Additional Comments", e); Institut für Mobile und Verteilte Systeme J. Luthiger 21 How to write Exceptions... public class MyException extends Exception { public MyException(String message) { super(message); public MyException(String message, Throwable t) { super(message, t); IMPORTANT: Decide when to use CHECKED or UNCHECKED Exception! Institut für Mobile und Verteilte Systeme J. Luthiger 22

Still to remember... Checked Exception are MUCH, MUCH better than every error code, as seen in languages like C and C++ (!) Institut für Mobile und Verteilte Systeme J. Luthiger 23 Asserts You can use assertions to detect errors that may otherwise go unnoticed. Assertions contain Boolean expressions that define the correct state of your program at specific points in the program source code. The Java SE platform 1.4, has introduced a built-in assertion facility. Institut für Mobile und Verteilte Systeme J. Luthiger 24

Design by contract Precondition: A condition that the caller of an operation agrees to satisfy The user invoking the computation has the responsibility of providing the correct input, which is a precondition. Postcondition: A condition that the method itself promises to achieve If the computation is successful, we say that the computation has satisfied the postcondition. Invariant: A condition that a class must satisfy anytime a client could invoke an object's method, or a condition that should always be true for a specified segment or at a specified point of a program Institut für Mobile und Verteilte Systeme J. Luthiger 25 Using Assertions Use the assert statement to insert assertions at particular points in the code. assert booleanexpression; assert booleanexpression : errormessage; Institut für Mobile und Verteilte Systeme J. Luthiger 26

Debugger see "Using the Eclipse Debugger" "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan Institut für Mobile und Verteilte Systeme J. Luthiger 27