Chapter 12 Exception Handling and Text IO Part 2

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Advertisements

1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
2. I/O Text Files CCSA225 - Advanced Java Programming Sarah Alodan
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 12 Exception Handling and Text.
1 Chapter 18 Exception Handling. 2 Motivations F Program runs into a runtime error –program terminates abnormally F How can you handle the runtime error.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Exception Handling.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Exception Handling and Text.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Exceptions and Assertions Chapter 15 – CSCI 1302.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved File I/O in java chapter.
CS 112 Programming 2 Lecture 08 Exception Handling & Text I/O (1)
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Chapter 9 Strings and Text I/O
Chapter 12 Exceptions and File Input/Output
Chapter 13 Exception Handling
CMSC 202 Text File I/O.
Chapter 12 Exception Handling And Text IO
Chapter 13 Exception Handling
Introduction to Exceptions in Java
Introduction to Exceptions in Java
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 – Part 3 Dr. Clincy Lecture.
Advanced Java Programming
Chapter 9 Strings and Text I/O
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.
Text I/O.
Unit-2 Objects and Classes
Chapter 11 Exception Handling and Text I/O
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO Part 1
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Presentation transcript:

Chapter 12 Exception Handling and Text IO Part 2

Pros & Cons of Exception Handling Advantage: Code is easier to understand and modify Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and to modify Drawback: Slower performance, higher resource requirement Be aware, however, that exception handling usually requires more time and resources because it requires instantiating a new exception object, rolling back the call stack, and propagating the errors to the calling methods

When to Use Exceptions Handling? Use try-catch blocks to deal with unexpected error conditions Do not use them to deal with simple, expected situations Example: 2nd block of code is preferable over the 1st try { System.out.println(refVar.toString()); } catch (NullPointerException ex) { System.out.println("refVar is null"); if (refVar != null) System.out.println(refVar.toString()); else System.out.println("refVar is null");

When to Throw Exceptions? When an exception occurs in a method, if we want the exception to be processed by its caller, we should create an exception object and throw it if we can handle the exception in the method where it occurs, there is no need to throw it

Exceptions are objects based on the superclass java.lang.Throwable Exception Types Exceptions are objects based on the superclass java.lang.Throwable

Custom Exception Classes Define custom exception classes only if Java’s predefined built-in classes are not sufficient Define custom exception classes by extending Exception or a subclass of Exception

Example: Custom Exception Class In Listing 12.7, setRadius() throws an exception if the radius is negative. Suppose you wish to pass the radius to the handler, you have to create a custom exception class InvalidRadiusException CircleWithRadiusException TestCircleWithRadiusException Run

The File Class The File class contains the methods for obtaining the properties of a file/directory and for renaming and deleting a file/directory File is intended to provide an abstraction that deals with most of the machine-dependent complexities of files and path names in a machine-independent fashion File is a wrapper class for the filename and its directory path A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing content from/to a file

The File Class

Example: Explore File Properties Objective: Write a program that demonstrates how to create files in a platform-independent way and use the methods in the File class to obtain their properties. The following figures show a sample run of the program on Windows and on Unix. TestFileClass Run

Text I/O A File object does not contain the methods for reading/ writing content from/to a file In order to perform I/O, you need to create objects using appropriate Java I/O classes We can read/write strings and numeric values from/to a text file using Scanner and PrintWriter class objects

Writing Data Using PrintWriter WriteData Run

Reading Data Using Scanner ReadData Run

Example: PrintWriter & Scanner Objective: Write a class named ReplaceText that replaces a string in a text file with a new string. The filename and strings are passed as command-line arguments as follows: java ReplaceText sourceFile targetFile oldString newString For example, invoking java ReplaceText s.txt t.txt apple orange replaces all the occurrences of apple by orange in s.txt and saves the new file in t.txt ReplaceText Run

Reading Data from the Web Just like we can read data from a file on your computer, we can also read data from a file on the Web

Reading Data from the Web URL url = new URL("http://google.com/index.html"); After a URL object is created, you can use openStream() defined in the URL class to open an input stream and use this stream to create a Scanner object as follows: Scanner input = new Scanner(url.openStream()); ReadFileFromURL Run