Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this method will crash your programm because we sayed "okay lets create a
- //int var (x) and read a double in it
- public void hello(){
- Scanner sc = new Scanner(System.in);
- int x = sc.nextDouble();
- }
- ______________________________________________________________________________________________
- //when you do this:
- public void hello() throws Exception{
- Scanner sc = new Scanner(System.in);
- int x = sc.nextDouble();
- }
- //you programm will not crash because you caught exception, but you dont know where its occured and which exception we have
- ___________________________________________________________________________________________________________________________
- public void hello(){
- Scanner sc = new Scanner(System.in);
- try(
- int x = sc.nextDouble();
- }catch(NumberFormatException e){
- e.printStackTrace();
- }
- }
- //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