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

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.