Darano

Untitled

Oct 10th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. //this method will crash your programm because we sayed "okay lets create a
  2. //int var (x) and read a double in it
  3. public void hello(){
  4.     Scanner sc = new Scanner(System.in);
  5.     int x = sc.nextDouble();
  6. }
  7. ______________________________________________________________________________________________
  8. //when you do this:
  9. public void hello() throws Exception{
  10.     Scanner sc = new Scanner(System.in);
  11.     int x = sc.nextDouble();
  12. }
  13.  
  14. //you programm will not crash because you caught exception, but you dont know where its occured and which exception we have
  15. ___________________________________________________________________________________________________________________________
  16. public void hello(){
  17.     Scanner sc = new Scanner(System.in);
  18.     try(
  19.         int x = sc.nextDouble();
  20.      }catch(NumberFormatException e){
  21.         e.printStackTrace();
  22.      }
  23. }
  24.  
  25. //with this way, we will exactly know where the exption was throws and what exception it was
Advertisement
Add Comment
Please, Sign In to add comment