package Example1;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionAgain {
static Scanner scanobj=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("how old are you");
int age=checktheage();
if(age!=0){
System.out.println(" you are "+age+" old");
}
}
public static int checktheage(){
try{
return scanobj.nextInt();
}
catch(InputMismatchException e){
System.out.println("Do not enter whole number");
return 0;
}
}
}
- We can add multiple catch block however the most appropriate exception should be mentioned first then the least excepted exception and then general exception.- If we want to ignore an exception then catch the exception but do not do anything inside the block example is shown below
catch(InputMismatchException e){}
- Catching multiple exception in single catch is also possible.
catch(InputMismatchException | IOException e){} >>>Finally
------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment