











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 19
This page cannot be seen from the preview
Don't miss anything!












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; }
throw
someThrowableObject
;
public Object pop()
throws EmptyStackException
{
Object obj;if (size == 0)
throw new EmptyStackException();
obj = objectAt(size - 1);setObjectAt(size - 1, null); size--;return obj;}
public
class Coin { public void read(BufferedReader in)throws IOException {value = Double.parseDouble(in.readLine());name
=in.readLine(); } }
Exception Specification
occur within the JRE, this
includes: arithmetic exceptions, pointerexceptions, and
indexing exceptions
Checked exceptions
are not runtime
exceptions and are checked by the compiler• Better to declare exception than to handle itincompetently
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
BufferedReader in = null; try
{ in = new BufferedReader(
new FileReader(filename)); purse.read(in); }^ finally
if (in !=null) in.close(); }
Example
PurseTest
interacts with user
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
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();
} }
^ 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.