Input Using Buffered Reader part 2-Data Transfer Programming-Lecture Slides, Slides of Computer Engineering and Programming

This lecture was delivered by Aniruddh Parmar at B. R. Ambedkar Bihar University for Data Transfer Programming course. It includes: Input, Buffered, Reader, Accessor, Mutator, State, Implicit, Parameter, Publish, Preconditions, Postcondition, Object

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dhananad
dhananad 🇮🇳

4

(4)

39 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Propagating Errors Up the Call Stack
Exceptions enable you to propagate error reporting up the call stack of
methods
method1 {
errorCodeType error;
error = call method2;
if (error) doErrorProcessing;
else proceed;
}
errorCodeType method2 {
errorCodeType error;
error = call method3;
if (error)
return error;
else proceed;
}
errorCodeType method3 {
errorCodeType error;
error = call readFile;
if (error) return error;
else proceed;
}
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Input Using Buffered Reader part 2-Data Transfer Programming-Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

Propagating Errors Up the Call Stack ^ Exceptions enable you to propagate error reporting up the call stack ofmethods

method

{ errorCodeType

error;

error = call method2;if^ (error)

doErrorProcessing;

else

proceed; } errorCodeType

method

{

errorCodeType

error;

error = call method3;if^ (error)return

error; else

proceed; } errorCodeType

method

{

errorCodeType

error;

error

=^ call

readFile;

if^ (error)

return

error;

else

proceed; }

Call Stack / Call Stack for

Exception

Grouping Error Types and Error

Differentiation

^ Exceptions fall intocategories orgroups.

Throwing Exceptions:

throw

clause

^ Before you can catch an exception, someJava code somewhere must throw one ^ Regardless of who (or what) throws theexception, it's always thrown with theJava

throw

statement

throw

someThrowableObject

;

^ In Java, throwable objects are instances ofany subclass of the

Throwable

public Object pop()

throws EmptyStackException

{

Object obj;if (size == 0)

throw new EmptyStackException();

obj = objectAt(size - 1);setObjectAt(size - 1, null); size--;return obj;}

Exception Specification

^ Catch

  • A method can catch an exception by providingan exception handler for that type of exception

^ Specify

  • If a method chooses not to catch an exception,the method must specify that it can throw thatexception

public

class Coin { public void read(BufferedReader in)throws IOException {value = Double.parseDouble(in.readLine());name

=in.readLine(); } }

  • Better to declare exception than to handle itincompetently

Exception Specification

^ Checked Exceptions

  • Java exceptions are of different typesincluding I/O Exceptions, runtime exceptions,and exceptions of your own creation•^ Runtime exceptions

occur within the JRE, this

includes: arithmetic exceptions, pointerexceptions, and

indexing exceptions

  • The compiler does not require that you catchor specify runtime exceptions•^

Checked exceptions

are not runtime

exceptions and are checked by the compiler• Better to declare exception than to handle itincompetently

Designing Your Own Exceptions

if (amount > balance)

throw new InsufficientFundsException(); ^ Make it an unchecked exception--programmercould have avoided it by calling

getBalance()

first  Extend RuntimeException and supply twoconstructors public class InsufficientFundsException extends RuntimeException {public InsufficientFundsException(){} public InsufficientFundsException(

String reason){super(reason); } }

Catching Exceptions

try

{ BufferedReader in = new BufferedReader(

new InputStreamReader(System.in)); System.out.print("How old are you?");String inputLine = in.readLine();int age = Integer.parseInt(inputLine);age++;^ System.out.println(

"Next year,you'll be " + age);

}^

catch

(IOException exception) { System.out.println(

"Input/output error " + exception);

}^ catch

(NumberFormatException exception){ System.out.println(

"Input was not a number");

}^ }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

The

finally

Clause

^ Executed when try block comes to normalendExecuted if a statement in try blockthrows an exception, before exception isthrown out of try block ^ Can also be combined with catch clauses

BufferedReader in = null; try

{ in = new BufferedReader(

new FileReader(filename)); purse.read(in); }^ finally

if (in !=null) in.close(); }

Example

^ Program

  • reads coin descriptions from file• adds coins to purse• prints total

^ What can go wrong?

  • File might not exist• File might have data in wrong format

^ Who can detect the faults?

  • main method of

PurseTest

interacts with user

  • main method can report errors• Other methods pass exceptions to caller

The

read

method of the

Purse

class

^ Unconcerned with exceptions ^ Just passes them to caller

public void read(BufferedReader in)

throws IOException {boolean done = false;while (! done) {

Coin c = new Coin();if (c.read(in)) {

add(c); } else {

done =true; } } }

The

readFile

method of the

Purse

class

^ finally clause closes files if exceptionhappens

public void readFile(String filename)

throws IOException {BufferedReader in = null;try {

in = new BufferedReader(

new FileReader(filename)); read(in); } finally {

if (in != null)

in.close();

} }

Scenario

^ PurseTest.main

calls

Purse.readFile

^ Purse.readFile

calls

Purse.read

^ Purse.read

calls

Coin.read

^ Coin.read

throws an

EOFException

^ Coin.read

has no handler for the exception and

terminates immediately.  Purse.read

has no handler for the exception and

terminates immediately  Purse.read

File has no handler for the exception

and terminates immediately after executing the finally

clause and closing the file.

^ PurseTest.main

has a handler for an

IOException

, a superclass of

EOFException

That handler prints a message to the user.