Q:

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

belongs to collection: Java Basic Programs

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to divide two numbers and catch the e... >>
<< Java program to count number of notes (rupees) in ...