tampurus

48 Exception size string n (7>n>18)

May 29th, 2022 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. import java.util.*;
  2. public class Main
  3. {
  4.     public static void main(String[] args) {
  5.         Scanner fn = new Scanner(System.in);
  6.         System.out.println("Enter a string ");
  7.         String s = fn.nextLine();
  8.        
  9.         int n = s.length();
  10.        
  11.         try{
  12.             if(n<7 || n>18){
  13.                 throw new StringIndexOutOfBoundsException("Lenth of String is not expected");
  14.             }
  15.             else {
  16.                 System.out.println("Your input is fine");
  17.             }
  18.         }
  19.         catch(StringIndexOutOfBoundsException e){
  20.             System.out.println(e);
  21.         }
  22.         finally{
  23.             System.out.println("This is from finally block");
  24.         }
  25.     }
  26. }
  27.  
  28. /*
  29. Enter a string
  30. suresh
  31. java.lang.StringIndexOutOfBoundsException: Lenth of String is not expected
  32. This is from finally block
  33. */
Add Comment
Please, Sign In to add comment