Advanced Java Programming

Slides:



Advertisements
Similar presentations
Yoshi
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
COMP 121 Week 5: Exceptions and Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
When you use an input or output file that does not exist, what will happen? −The compiler insists that we tell it what the program should do in such case.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
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.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Object Throwable ErrorException RuntimeException.
Java Exceptions a quick review….
Java Programming Fifth Edition
Chapter 12 Exception Handling And Text IO
Introduction to Exceptions in Java
Chapter 12 Exception Handling and Text IO
CSE 501N Fall ’09 17: Exception Handling
Exceptions 10-Nov-18.
What/how do we care about a program?
Exception Handling Chapter 9.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Topics Introduction to File Input and Output
CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Chapter 13 Exception Handling
Java Exceptions Dan Fleck CS211.
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
I/O Exceptions & Working with Files
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Java Basics Exception Handling.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Presentation transcript:

Advanced Java Programming Session #, Speaker Name Advanced Java Programming CSS446 Spring 2014 Nan Wang 08/24/11

Chapter Goals Read and write text files Process command line arguments Throw and catch exceptions Implement programs that propagate checked exceptions

Reading and Writing Text Files Use the Scanner class for reading text files Use the File class to describe disk files and directories. Use the PrintWriter class to write text files Close the Scanner and PrintWriter before done processing file (Some of the output may not be written to the disk file) FileNotFoundException occurs when input file or output file does not exists for a scanner When PrintWriter can not open the file to write Label the main method with a throws declararion

What happens when you supply the same name for the input and output files to the Total program? What happens when you supply the name of a nonexistent input file to the Total program?

Common Error (1) Backslashes in File Names

Common Error (2) Constructing a Scanner with a String

Command Line Arguments

Exception Handling Two way to deal with the program errors: Detection Handling To signal an exceptional condition, use the throw statement to throw an exception object.

Catching Exceptions

Internal Errors Internal errors are reported by descendants of the type Error. One example is the OutOfMemoryError, which is thrown when all available computer memory has been used up. These are fatal errors that happen rarely, and we will not consider them in this book.

Unchecked Exceptions Descendants of RuntimeException, such as as IndexOutOfBoundsException or Illegal­ ArgumentException indicate errors in your code. They are called unchecked exceptions. And it is your fault

Checked Exceptions All other exceptions are checked exceptions. These exceptions indicate that something has gone wrong for some external reason beyond your control. No matter how careful you are, it happens! 

The throws Clause

The finally Clause Once a try block is entered, the statements in a finally cause are guaranteed to be executed, whether or not an exception is thrown.

Designing Your own Exception Types Describe an error condition, provide a subclass of an existing exception class.