CS 190 Lecture Notes: Exception Handling

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Advertisements

Building HTTP clients in PHP. A PHP package for sending HTTP requests and getting responses A PHP package for handling HTTP requests/responses is available.
Chapter 1 Writing a Program Fall Class Overview Course Information –On the web page and Blackboard –
J-Unit Framework.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
CS 2110 Software Design Principles II Based on slides originally by Juan Altmayer Pizzorno port25.com.
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.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
Handling errors Writing robust code. 16/12/2004Lecture 10: Handling Errors2 Main concepts to be covered Defensive programming. –Anticipating that things.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
Two Ways to Store Data in a File Text format Binary format.
CS 190 Lecture Notes: Tweeter ProjectSlide 1 Uniform Resource Locators (URLs) Scheme Host.
Servlet Communication Other Servlets, HTML pages, objects shared among servlets on same server Servlets on another server with HTTP request of the other.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
Reading/Writing Files, Webpages CS2110, Recitation 8 1.
Heaps and Heap Sorting Heaps implemented in an Array Heap Sorting.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 121 – Intro to Programming:Java - Lecture 12 Announcements New Owl assignment up soon (today?) For this week: read Ch 8, sections 0 -3 Programming assignment.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Introduction to Exceptions in Java CS201, SW Development Methods.
1 Framework Presentation Project Participants: Karun Biyani Manish Mehta Pradeep Vincent CSE870 Advanced Software Engineering, Spring 2001 Instructor:
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Software Construction Lab 10 Unit Testing with JUnit
WARNING 20 min These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint.
16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Introduction to Exceptions in Java
Strings and File I/O.
Introduction to Exceptions in Java
Accessing Files in Java
CS Week 14 Jim Williams, PhD.
Uniform Resource Locators
CS 190 Lecture Notes: Exception Handling
Bowling Game Kata Object Mentor, Inc.
Generating the Server Response: HTTP Status Codes
Clients and Servers 19-Nov-18.
null, true, and false are also reserved.
CS 190 Lecture Notes: Tweeter Project
Asynchronous Javascript And XML
Defining New Types of Objects, part 3
Heaps and Heap Sorting Heaps implemented in an Array Heap Sorting.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Clients and Servers 1-Dec-18.
Credit to Eclipse Documentation
Unit 6 Working with files. Unit 6 Working with files.
HTTP and Sockets Lecture 6
Exceptions Complicate Code
Exceptions Complicate Code
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
CMSC 202 Exceptions 2nd Lecture.
CSE 403 Lecture 17 Coding.
Uniform Resource Locators (URLs)
Building Java Programs
JavaScript Reserved Words
Exceptions Complicate Code
Sadalage & Fowler (Amazon)
Uniform Resource Locators
CSE 143 Lecture 5 More ArrayIntList:
Method exercises Without IF
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Uniform Resource Locators (URLs)
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Exceptions and Exception Handling
Presentation transcript:

CS 190 Lecture Notes: Exception Handling Junit Test Example package stanford.cs190.tweeter; import org.junit.Test; public class JsonTest extends junit.framework.TestCase { @Test public void test_array() { Json document = new Json(); document.startArray(); document.addValue(12345); document.addValue(999); document.endArray(); assertEquals("[12345, 999]", document.toString()); } ... Do something Check result CS 190 Lecture Notes: Exception Handling

CS 190 Lecture Notes: Exception Handling Test Isomorphism protected void readRequest(Reader reader) { try { BufferedReader in = new BufferedReader(reader); // Read the first line of input // (method, url, protocol version). String firstLine = in.readLine(); if (firstLine == null) { throw new RequestError("empty HTTP request"); } String[] words = firstLine.split(" +", 4); if (words.length < 3) { throw new RequestError("syntax error in " + “first line: '%s'", firstLine); method = words[0]; rawUrl = words[1]; protocolVersion = words[2]; // Read the headers. Each iteration through the // following loop reads one line, which contains a // single header. while (true) { String headerLine = in.readLine(); if (headerLine == null) { throw new RequestError( "unexpected EOF while reading " + "headers"); if (headerLine.length() == 0) { // Empty line marks the end of headers. break; int i = headerLine.indexOf(": "); if (i == -1) { + "header line: '%s'", headerLine); headers.put(headerLine.substring(0, i), headerLine.substring(i+2)); ... @Test public void test_readRequest_basics() { ... } public void test_readRequest_emptyInput() { public void test_readRequest_incompleteFirstLine() { public void test_readRequest_EofInHeaders() { public void test_readRequest_headerHasNoColon() { CS 190 Lecture Notes: Exception Handling

CS 190 Lecture Notes: Exception Handling Helper Methods /** * Create a bunch of new tweets in the current repo. * @param count * Number of tweets to create. */ protected void createTweets(int count) { ... } * Returns a string containing a sorted list of the * tweet ids for each of the TweetFiles in the * in-memory cache. protected String getCachedIds() { CS 190 Lecture Notes: Exception Handling

CS 190 Lecture Notes: Exception Handling Helper Methods, cont’d @Test public void test_checkCacheSize_belowLimit() { createTweets(17); repo.cacheLimit = 3; repo.checkCacheSize(); assertEquals("10 20", getCachedIds()); } CS 190 Lecture Notes: Exception Handling