Tuesday, September 8, 2009

Design with Java Exceptions

Exceptions have several benefits. They allow you to separate error handling code from normal code.


Here is an application that uses FileInputStream to print the text of a file to the standard output.




// In source packet in file except/ex9/Example9a.java

import java.io.*;

class Example9a {

public static void main(String[] args)

throws IOException {

if (args.length == 0) {

System.out.println("Must give filename as first arg.");

return;

}

FileInputStream in;

try {

in = new FileInputStream(args[0]);

}

catch (FileNotFoundException e) {

System.out.println("Can't find file: " + args[0]);

return;

}

int ch;

while ((ch = in.read()) != -1) {

System.out.print((char) ch);

}

System.out.println();

in.close();

}

}


Click Here to Know More... 

 

0 comments:

Can You Win This Game?