fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. int flag = 0;
  14. while (flag != 1) {
  15. try {
  16. System.out.println("enter two integers");
  17. int a = sc.nextInt();
  18. int b = sc.nextInt();
  19. int result = a + b;
  20. flag = 1;
  21. System.out.println("ans is " + result);
  22.  
  23. } catch (NumberFormatException e) {
  24. System.out.println("exception caught");
  25. } catch (InputMismatchException e) {
  26. System.out.println("exception caught");
  27. } finally {
  28. sc.nextLine();
  29. }
  30. }
  31. }
  32. }
Runtime error #stdin #stdout #stderr 0.17s 321344KB
stdin
f g 
2 3
stdout
enter two integers
exception caught
enter two integers
ans is 5
stderr
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1540)
	at Ideone.main(Main.java:28)