Should anyone ever catch IOException / exception if a program is reading in a txt file using scanner object, But does a FileNotFoundException generate?
Should such additional codes be unnecessary or significant?
Import java.io.File; Import java.io.phileNotFoundException; Import java.util.Scanner; Public class test scanner {public trial (scanner) {readFile ("dummy test"); } Public Zero ReadFile (string filepath) {file file = new file (file path); Scanner scanner = null; {Scanner = New Scanner (File) Try; While (scanner.hasNext ()) {string line = scanner.nxtain (); If (line.matches ("^. + @ Yahoo \\.com? (? Uk)? $")) System.out.println (line); }} Hold (FileNotFoundException e) {System.out.println ("error: can not load" + filepath); E.printStackTrace (); } Finally {if (scanner! = Null) scanner.clos (); My rule of thumb is to go wide with the exception of catching exceptions (to catch exceptions)}}}
Wrench "What About Runtime Upspace" in this approach allows a school runtime to bubble exception. But what do I think is that in the circumstances where I am catching exceptions, I want them all ... and sometimes throbbles (I was burned because of the exception not catching and throttling).
Some examples:
Public zero myMethod () throws IOException, FileNotFoundException ();
In a situation where I do not want to bubble exception (all of them need to deal with)
try {myMethod (); Hold (exception e) {// handle it}
In a situation where I'm holding an exception and I have to do something different for FileNotFound.
try {MyMethod (); Catch (filenoutfound exception fee) {// handle file not found} Catch (exception E) {// handle it}
In a situation where I am giving the exception to the bubbles because I know That's some very cool exception handling code in the series that is handling it and I want to avoid signing exceptions several times:
myMethod ();
In a situation where I left the fileNotFound except for the exception bubble.
try {myMethod (); Catch (FileNotFoundException fe) {// handle file not found}
No comments:
Post a Comment