Question

JAVA Problem Description: For this assignment, you will be writing your own exception class and you...

JAVA Problem Description:

For this assignment, you will be writing your own exception class and you will handle run-time exceptions resulting from one particular input situation.

Specifics:

1) Design and implement a program that has an exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it.

2) In the main driver of the program (call this class MyExceptionTest), read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (use 20 or more), throw the exception.

3) The exception is to be caught and handled by having an appropriate message output to the screen. Once done, the program continues by processing more strings.

try using string.length

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Follow the below java code to write user defined exception and how to call them

User defined Exception Class StringTooLongException:

import java.util.*;
class StringTooLongException extends Exception // Exception is the parent class for all type of exceptions
{
public StringTooLongException(String s)
{
super(s);
}
public String toString(){
return ("Too many Characters in the given string") ; //this is user defined message you can replace with your own
}
}

// Main class MyExceptionTest:
public class MyExceptionTest{
public static void main(String [] args){
String str = "";
Scanner sc = new Scanner(System.in);
for(int i=1;i>0;i++){ //this loop got terminated when it get DONE string from user
str = sc.next();
if(str.equals("DONE")){
break;
}
  
try{
if(str.length()>=20)

// .length() function gives the string length

throw new StringTooLongException(str);

//here the user defined exception is get called

/* you can write your own code when the string is less than 20 characters of length */

}
catch(StringTooLongException exception){ // user defined message gets returned from the user defined exception
System.out.println(exception);
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
JAVA Problem Description: For this assignment, you will be writing your own exception class and you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...

    (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when a string is discoveredthat has too many characters in it. In the main driver of the program, read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (say 20), throw, catch, and handle the exception. The exception is handled by printing an appropriate message, and the programwill continueprocessing more strings.

  • java punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates...

    java punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates an exception class called StringTooLongException, designed to be thrown when a string is discovered that too many characters in it. In the main driver of the program, read strings from th user until the user enters "DONE". If a string is entered that has too many chara (say 20), throw the exception. Allow the thrown exception to terminate the program.

  • Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException, PunctuationException, and CommaException. EndOfSentenceException is...

    Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException, PunctuationException, and CommaException. EndOfSentenceException is the parent class to PunctuationException, which is the parent class to CommaException. Test your classes in a Driver class with a main method that asks the user to input a sentence. If the sentence ends in anything except a period (.), exclamation point (!), or question mark (?), the program should throw a PunctuationException. If the sentence specifically ends in a comma, the program...

  • JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour...

    JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock like 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). Provide a default constructor, a set method which is given a String (eg, “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate (in your opinion) to the time. Please see the NOTE below concerning input validation and...

  • With Java Language: In this assignment, you are to create a class named Payroll. In the...

    With Java Language: In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) .İd: String (5 pts) hours: int (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an...

  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is...

    QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is no catch block handling the same data type as the exception thrown execution continues if the catch block handles the same data type as the exception thrown 10 points    QUESTION 2 An exception is handled using an if-else statement an assignment statement a loop statement a try-catch block 10 points    QUESTION 3 In program 9.4, what is the data type being thrown?...

  • Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...

    Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT