Why should I use Exception handlings in Java? -
i read many posts on java exception handling did satisfy answer yet.why should put them in codes ?
- i use api methods of jre , these made checked exceptions. if want use them , need throw or catch exceptions (eg.java i/o). reasonable rules use exceptions in class ?
- i heard
java exception handlings make separating error handling codes business logic
where separating error handling in below snippet ?
public int division(int divident, int divisor) { int result = 0; try { result = divident / divisor; } catch (arithmeticexception e) { system.out.println("divisor must not 0 !"); } return result; }
3. java's default exception handling makes showing exception information in standard output , terminate program. use exception handling myself avoid terminating programs?
it's 3 questions, it's not that's first time that's happened here. :-)
yes, must handle them or declare them, because they're checked exceptions. reason must code calling code knows ways in code may fail (because you've declared exceptions can throw).
that snippet simple, separation , benefit aren't clear. consider opening 2 streams, copying 1 another, transformation code (including calls subordinate methods). end method body 10-20 statements in it. instead of every i/o statement having check whether worked or not, write logic wrapped in
ioexception
handler, knowing i/o exceptions jump out of main logic handler.it depends on kind of program you're writing, in general handle exceptions @ appropriate level, typically @ multiple levels in program. outermost level, deal really, unusual, unrecoverable exceptions, either let default handling does, or use catch-all handler similar might (attempt to) record failure elsewhere (like log file) well:
public class myprogram { public static final void main(string[] args) { try { // run... } catch (throwable t) { // handle fact went wrong here, if can // really, unusual errors, // otherwise have handled them earlier } } }
to underscore point in #2, consider 2 process
methods, 1 in java exceptions, , other in hypothetical java-like language doesn't have exceptions:
the java one:
private void process() { try ( // <== main logic reader fr = new filereader(this.sourcefilename); // <== main logic bufferedreader br = new bufferedreader(fr); // <== main logic writer fw = new filewriter(this.destfilename); // <== main logic bufferedwriter bw = new bufferedwriter(fw) // <== main logic ) { // <== main logic string line; // <== main logic while ((line = br.readline()) != null) { // <== main logic if (shouldincludeline(line)) { // <== main logic line = transformline(line); // <== main logic bw.write(line); // <== main logic bw.newline(); // <== main logic } // <== main logic } // <== main logic } catch (filenotfoundexception fnfe) { // <== error handling // couldn't find file // <== error handling // (handle it) // <== error handling } // <== error handling catch (ioexception ioe) { // <== error handling // i/o error // <== error handling // (handle it) // <== error handling } // <== error handling catch (exception e) { // <== error handling // else went wrong // <== error handling // (handle it) // <== error handling } // <== error handling }
the hypothetical java-like language without exceptions one:
// fake, pseudo-java private errors process() { reader fr = new filereader(this.sourcefilename); // <== main logic if (fr == null) { // <== error handling return errors.cantopensource; // <== error handling } // <== error handling bufferedreader br = new bufferedreader(fr); // <== main logic writer fw = new filewriter(this.destfilename); // <== main logic if (fw == null) { // <== error handling br.close(); // <== error handling return errors.cantopendest; // <== error handling } // <== error handling bufferedwriter bw = new bufferedwriter(fw) // <== main logic string line; // <== main logic while ((line = br.readline()) != io.end_of_file) { // <== main logic if (line == null) { // <== error handling br.close(); // <== error handling bw.close(); // <== error handling return errors.cantread; // <== error handling } if (shouldincludeline(line)) { // <== main logic line = transformline(line); // <== main logic if (bw.write(line) == -1 || bw.newline() == -1) { // <== main logic (plus error handling) br.close(); // <== error handling bw.close(); // <== error handling return errors.cantwrite; // <== error handling } } } bw.close(); br.close(); return errors.success; }
notice:
- how main logic littered error handling, making harder read , follow.
- special "error" return values necessary method might possibly have kind of failure mode. had add 1
process
, , checknull
new filereader
, such, , check -1 read , write ops, etc.
if you're interested, here's full version of java program vs. full version of not-really-java program:
java:
import java.io.*; public class example { private string sourcefilename; private string destfilename; public static void main (string[] args) throws java.lang.exception { try { new example(args[0], args[1]).process(); } catch (arrayindexoutofboundsexception npe) { // bit of exaggeration, i'd check in advance, since user not // supplying arguments isn't "exceptional" condition. system.out.println("usage: java example [source file name] [dest file name]"); } } public example(string src, string dest) { // similar, these checks assertions, i'm making point... if (src == null || src.length() == 0) { throw new illegalargumentexception("src must non-null , non-blank"); } if (dest == null || dest.length() == 0) { throw new illegalargumentexception("dest must non-null , non-blank"); } this.sourcefilename = src; this.destfilename = dest; } private void process() { try ( // <== main logic reader fr = new filereader(this.sourcefilename); // <== main logic bufferedreader br = new bufferedreader(fr); // <== main logic writer fw = new filewriter(this.destfilename); // <== main logic bufferedwriter bw = new bufferedwriter(fw) // <== main logic ) { // <== main logic string line; // <== main logic while ((line = br.readline()) != null) { // <== main logic if (shouldincludeline(line)) { // <== main logic line = transformline(line); // <== main logic bw.write(line); // <== main logic bw.newline(); // <== main logic } // <== main logic } // <== main logic } catch (filenotfoundexception fnfe) { // <== error handling // couldn't find file // <== error handling // (handle it) // <== error handling } // <== error handling catch (ioexception ioe) { // <== error handling // i/o error // <== error handling // (handle it) // <== error handling } // <== error handling catch (exception e) { // <== error handling // else went wrong // <== error handling // (handle it) // <== error handling } // <== error handling } private boolean shouldincludeline(string line) { return line.length() != 0; } private string transformline(string line) { return line.touppercase(); } }
the hypothetical java-like language without exceptions one:
// fake, pseudo-java without exceptions, isn't real import java.io.*; public class example { private string sourcefilename; private string destfilename; private enum errors { success, cantopensource, cantopendest, cantread, cantwrite } public static void main (string[] args) throws java.lang.exception { if (args.length < 2) { system.out.println("usage: java example [source file name] [dest file name]"); } if (args[0] == null || args[0].length() == 0) { throw new illegalargumentexception("src must non-null , non-blank"); } if (args[1] == null || args[1].length() == 0) { throw new illegalargumentexception("dest must non-null , non-blank"); } switch (new example(args[0], args[1]).process()) { case errors.cantopensource: // handle break; case errors.cantopendest: // handle break; case errors.cantread: // handle break; case errors.cantwrite: // handle break; } } public example(string src, string dest) { // not how constructor trusting called valid arguments this.sourcefilename = src; this.destfilename = dest; } private errors process() { reader fr = new filereader(this.sourcefilename); // <== main logic if (fr == null) { // <== error handling return errors.cantopensource; // <== error handling } // <== error handling bufferedreader br = new bufferedreader(fr); // <== main logic writer fw = new filewriter(this.destfilename); // <== main logic if (fw == null) { // <== error handling br.close(); // <== error handling return errors.cantopendest; // <== error handling } // <== error handling bufferedwriter bw = new bufferedwriter(fw) // <== main logic string line; // <== main logic while ((line = br.readline()) != io.end_of_file) { // <== main logic if (line == null) { // <== error handling br.close(); // <== error handling bw.close(); // <== error handling return errors.cantread; // <== error handling } if (shouldincludeline(line)) { // <== main logic line = transformline(line); // <== main logic if (bw.write(line) == -1 || bw.newline() == -1) { // <== main logic (plus error handling) br.close(); // <== error handling bw.close(); // <== error handling return errors.cantwrite; // <== error handling } } } bw.close(); br.close(); return errors.success; } private boolean shouldincludeline(string line) { return line.length() != 0; } private string transformline(string line) { return line.touppercase(); } }
Comments
Post a Comment