Java Program to Read Marks Between 1 to 100 (An Example of Exceptional Handling)

You are currently viewing Java Program to Read Marks Between 1 to 100 (An Example of Exceptional Handling)

Java program to read marks between 1 to 100 (An Example of Exceptional Handling)

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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply