Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can manage an exception <em>within</em> a method using <code>try</code> and <code>catch</code> as you say. In that case, you do not need to use <code>throws</code>. For example: </p> <pre><code>public void myMethod() { try { /* Code that might throw an exception */ } catch (SpaceInvadersException exception) { /* Complicated error handling code */ } } </code></pre> <p>But suppose you had a thousand methods, all of which might throw a <code>SpaceInvadersException</code>. Then you would end up having to write all the complicated error handling code a thousand times. Of course, you could always create an <code>ErrorHandler</code> class with a <code>dealWithSpaceInvadersException()</code> method that you could call from them, but you'd still be stuck with a thousand <code>try</code>-<code>catch</code> blocks. </p> <p>Instead, you tell the compiler that each of these thousand methods could throw a <code>SpaceInvadersException</code>. Then any method that calls one of these methods needs to deal with the error itself, either by using a <code>try</code>-<code>catch</code>, or by telling the compiler that <em>it</em> might throw a <code>SpaceInvadersException</code>. This is done using the <code>throws</code> keyword, like this: </p> <pre><code>public void myMethod() throws SpaceInvadersException { /* Code that might throw an exception */ } public void callingMethod() { try { myMethod(); } catch (SpaceInvadersException exception) { /* Complicated error handling code */ } } </code></pre> <p>In that case, you need to inform the compiler that <code>myMethod</code> could throw a <code>SpaceInvadersException</code>. This means that you can't call that method without dealing with the exception in some way (<code>try</code>-<code>catch</code> or using the <code>throws</code> keyword on the calling method). If <code>throws</code> weren't there, you could call the method without doing any exception handling, and get an exception that wasn't dealt with anywhere in the program (which would be bad). </p> <p>Since it is always better to avoid code duplication, it is normally better to palm off your error handling to a <code>try</code>-<code>catch</code> in a much higher level function than it is to deal with it separately in all of the low level methods. That is why this mechanism exists. </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. 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