90.2k views
1 vote
Design and implement a program that creates an exception class called InvalidDocumentCodeException, designed to be thrown when an improper designation for a document is encountered during processing. Suppose that in a particular business, all documents are given a two-character designation starting with U, C, or P, which stand for unclassified, confidential, or proprietary. If a document designation is encountered that doesn’t fit that description, the exception is thrown. Create a driver program to test the exception, allowing it to terminate the program.

1 Answer

4 votes

Answer:

See Explaination

Step-by-step explanation:

import java.util.Scanner;

public class DocumentCodeTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the designation: ");

String designation = scan.next();

try{

if (designation != null

&& (designation.charAt(0) == 'U'

|| designation.charAt(0) == 'C' || designation

.charAt(0) == 'P') && designation.length() == 2) {

System.out.println("Valid designation");

}

else{

throw new InvalidDocumentCodeException("Invalid designation. Designation must be two characters and start with U, C or P.");

}

}

catch(InvalidDocumentCodeException e){

System.out.println(e);

}

}

}

InvalidDocumentCodeException.java

public class InvalidDocumentCodeException extends Exception{

String errorMsg ;

public InvalidDocumentCodeException(String s){

this.errorMsg = s;

}

public String toString(){

return (errorMsg ) ;

}

}

User TheSprinter
by
5.3k points