return from catch
hai
i found return statement in catch block, can you please tell why is required, i suppose that if proper file names are given then no exception will occur, catch wont be executed so the program runs automatically, so pls tell me why return is required in catch
import java.io.*;
class ShowFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF is encountered
do {
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}
bye............
thanx in advance
> please tell why is required, i suppose that if proper
> file names are given then no exception will occur,
If you give a method valid data, it will not throw an exception.
When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
mlka at 2007-7-15 18:12:02 >

> When you post code, please use [code] and
> [/code] tags as described in
> [url=http://forum.java.sun.com/help.jspa?sec=formattin
> g]Formatting tips[/url] on the message entry
> page. It makes it much easier to read.
But not more leasurable. Catching and returning from an AIOBE, but throwing IOException from main. Give me a break.
1.) The main method shouldn't be declared to throw exceptions.
2.) Use main only for bootstrapping an application, not for doing all the job.
3.) Instead of putting a return statement into the catch block, you could as well go on reading in data inside of the try block right after creating the stream instance.
4.) Put the close operation into a finally clause.