Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is one more option if the exception handling doesn't need to happen in the <code>getContents</code> method -- add a <code>throws</code> clause to the method to make the method throw an exception:</p> <pre><code>public static String getContents (File file) throws IOException, FileNotFoundException { </code></pre> <p>This way, the code calling the method will handle the <code>Exception</code>s rather than the method itself. There will be no need for <code>try</code>/<code>catch</code> blocks in that method, if the <code>Exception</code>s are thrown up to the methods that called it.</p> <p>This may or may not be the desired way to handle the situation, depending on how the method is expected to behave.</p> <p><strong>Edit</strong></p> <p>Upon second thought, making the method throw an exception may be a good idea. I think D.Shawley's comment I think sums it up well -- "exception handling should mean only handling the exception where it makes sense."</p> <p>In this case, the <code>getContents</code> method appears to get the contents of a specified <code>File</code>, and returns a <code>String</code> to the caller.</p> <p>If the exception handling were to be performed in the <code>getConents</code> method, the only way to communicate that an error has occurred is by returning some kind of pre-determined value, such as <code>null</code> to the caller to inform that an error occurred.</p> <p>However, by having the method itself throw an exception back to the caller, the caller can choose to react accordingly:</p> <pre><code>try { String contents = getContents(new File("input.file")); } catch (IOException ioe) { // Perform exception handling for IOException. } catch (FileNotFoundException fnfe) { // Inform user that file was not found. // Perhaps prompt the user for an alternate file name and try again? } </code></pre> <p>Rather than having the <code>setContents</code> method come up with its own protocol for notifying that an error occurred, it would probably be better to throw the <code>IOException</code> and <code>FileNotFoundException</code> back to the method caller, so the exception handling can be performed at a place where the appropriate alternate actions can take place.</p> <p>Exception handling should only be performed if some meaningful handling can take place.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload