Note that there are some explanatory texts on larger screens.

plurals
  1. POException Handling try-catch statement String Length
    text
    copied!<p>Hey everyone I have a question pertaining to a exception try-catch statement. In a practice problem I am doing, the user inputs a string of a certain length. If the user enters a string greater than the length of 20, an exception is thrown. Now I seem to have everything setup in order, but what is really confusing me is what to put in the try block. Can anyone explain in either pseudo code or through explanation in what I need to input to get it to run?</p> <p>Also, I have a question pertaining to the catch statement with my <code>catch(StringTooLongException e)</code> . I already made two other programs that deal with an inherited class and a class that uses the name I created to solve this same problem with out a try-catch statement. That is where the <code>StringTooLongException</code> comes from. My question is, how do you know what exception name to use? I know there are general exceptions built into java, but I am just slightly confused.</p> <p>Thanks</p> <p>Here is my code:</p> <pre><code>import java.util.Scanner; public class StringTooLongExceptionModified{ public static void main(String[] args){ String input; Scanner myScan = new Scanner(System.in); System.out.println("Enter a string(DONE to quit): "); input = myScan.nextLine(); while(!input.equals("DONE")){ try{ } catch(StringTooLongException e){ System.out.println ("Exceeds string length: " + input); } System.out.println("Enter a string(DONE to quit): "); input = myScan.nextLine(); } } } </code></pre>
 

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