Home » Java programs » Java basic programs

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



An Example of Exceptional Handing: In this java program we are implement a program in which we are reading marks of a student between 1 to 100 and if marks are out of range exceptional will be returns.
Submitted by Chandra Shekhar, on January 15, 2018

In this program, we have to read marks between 1 to 100 with checking exceptions using java program.

Example:

    Input:
    Enter marks: 80
    Output:
    Entered marks are:  80


    Input:
    Enter marks: 120
    Output:
    Error:ExceptionHandling.StudentManagement: Invalid marks:120

Java program

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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.