Craeting a custom exception class begins by extending the built-in Exception class. This allows you to define application-specific error conditions that can be thrown and caught programmatically.
The following implementation demonstrates a custom exception that leverages the parent class constructor to store error messages:
1 public class GradeException extends Exception {
2
3 public GradeException(String errorMessage) {
4 super(errorMessage);
5 }
6 }
This custom exception can be utilized to validate whether user input represents a valid integer score. If validation fails, the exception is thrown with an appropriate message; otherwise, the program proceeds to determine the corresponding grade level.
When working with try-catch blocks, any exception thrown by the code must be explicitly propagated using the throw keyword before it can be caught by a catch block. The key requirement is that potentially problematic code must be enclosed within a try block. A single try block may be followed by multiple catch blocks, but once an exception matches one catch block, subsequent catch blocks will not execute.
1 import java.util.Scanner;
2
3 public class GradeProcessor {
4 static Scanner input = null;
5
6 static int validate(String value) throws GradeException {
7 if (!value.matches("^\\-?[0-9]+$")) {
8 throw new GradeException("Invalid input: integers only!");
9 }
10 int number = Integer.valueOf(value);
11 if (number > 100 || number < 0) {
12 throw new GradeException("Score must be between 0 and 100!");
13 }
14
15 return number;
16 }
17
18 public static void main(String[] args) {
19 System.out.println("Enter an integer score:");
20 String userInput = null;
21
22 input = new Scanner(System.in);
23 userInput = input.nextLine();
24 try {
25 int score = validate(userInput);
26 if (score >= 90 && score <= 100) {
27 System.out.println("Grade: A - Excellent!");
28 }
29 else if (score >= 80 && score < 90) {
30 System.out.println("Grade: B - Good!");
31 }
32 else if (score >= 70 && score < 80) {
33 System.out.println("Grade: C - Average!");
34 }
35 else if (score >= 60 && score < 70) {
36 System.out.println("Grade: D - Passing!");
37 }
38 else {
39 System.out.println("Grade: F - Failing!");
40 }
41 }
42 catch (GradeException error) {
43 System.out.println(error.getMessage());
44 }
45 }
46 }
The complete try-catch structure optionally includes a finally block, which executes regardless of whether an exception occurred. However, the finally block will NOT execute under these specific circumstances:
- An unhandled exception occurs within the finally block itself
- System.exit(0) is called in preceding code
- The thread executing the code terminates
- The CPU is powered off
The following example demonstrates the addition of a finally block to the previous code:
1 import java.util.Scanner;
2
3 public class GradeProcessor {
4 static Scanner input = null;
5
6 static int validate(String value) throws GradeException {
7 if (!value.matches("^\\-?[0-9]+$")) {
8 throw new GradeException("Invalid input: integers only!");
9 }
10 int number = Integer.valueOf(value);
11 if (number > 100 || number < 0) {
12 throw new GradeException("Score must be between 0 and 100!");
13 }
14
15 return number;
16 }
17
18 public static void main(String[] args) {
19 System.out.println("Enter an integer score:");
20 String userInput = null;
21
22 input = new Scanner(System.in);
23 userInput = input.nextLine();
24 try {
25 int score = validate(userInput);
26 if (score >= 90 && score <= 100) {
27 System.out.println("Grade: A - Excellent!");
28 }
29 else if (score >= 80 && score < 90) {
30 System.out.println("Grade: B - Good!");
31 }
32 else if (score >= 70 && score < 80) {
33 System.out.println("Grade: C - Average!");
34 }
35 else if (score >= 60 && score < 70) {
36 System.out.println("Grade: D - Passing!");
37 }
38 else {
39 System.out.println("Grade: F - Failing!");
40 }
41 }
42 catch (GradeException error) {
43 System.out.println(error.getMessage());
44 }
45 finally {
46 System.out.println("Validation process completed.");
47 }
48 }
49 }