package ExceptionHandling;
import java.util.Scanner;
import java.util.InputMismatchException;
// create student class.
class StudentManagement extends Exception
{
StudentManagement(String error)
{
super(error);
}
}
public class MyException
{
public static void main(String arg[])
{
try
{
// create object of scanner class.
Scanner KB=new Scanner(System.in);
// enter marks between 1-100.
System.out.print("Enter marks here : ");
int h=KB.nextInt();
// condition for checking valid entry of marks.
if(!(h>=0 && h<=100))
{
throw(new StudentManagement("Invalid marks:"+h));
}
System.out.print("Entered marks are : " + h);
}
catch(InputMismatchException e)
{
System.out.println("Invalid Input..Pls Input Integer Only..");
}
catch(StudentManagement e)
{
System.out.println("Error:"+e);
}
}
}
Output
First run:
Enter marks here : 80
Entered marks are : 80
Second run:
Enter marks here : 120
Error:ExceptionHandling.StudentManagement: Invalid marks:120
Java program
Output
need an explanation for this answer? contact us directly to get an explanation for this answer