Advertisement
jasonpogi1669

Create Custom Exception (Classes) using Java

Mar 1st, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package main;
  7.  
  8. /**
  9.  *
  10.  * @author markjasongalang
  11.  */
  12. import java.util.Scanner;
  13.  
  14. class InvalidGradeException extends Exception {
  15.     public String PrintErrorMessage() {
  16.         return "Grade should be between 0 to 100 only.";
  17.     }
  18. }
  19.  
  20. public class Main {
  21.     public static void main(String[] args) {
  22.         Scanner in = new Scanner(System.in);
  23.         try {
  24.             System.out.print("Enter grade: ");
  25.             int grade = in.nextInt();
  26.             if (grade < 0 || grade > 100) {
  27.                 throw new InvalidGradeException();
  28.             }
  29.             System.out.println("Grade: " + grade);
  30.         } catch (InvalidGradeException e) {
  31.             System.out.println(e.PrintErrorMessage());
  32.         }
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement