Question

In: Computer Science

CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create...

CREATE A NEW Java PROGRAM that demonstrates :

1.1 numeric and 1 string exception

2. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.

Solutions

Expert Solution

Java Program:

//Exception Class
class UserException extends Exception{
   String str1;
   // Constructor
   UserException(String val) {
       //Storing message
       str1=val;
   }
   //Method that prints the message
   public String toString(){
       return ("UserException Occurred: " + str1) ;
   }
}

class ExceptionDemonstrater {
   //Main method
   public static void main(String[] args) {
      
       //Number Exception
       try
       {
           //Trying to convert String to integer - Generates Exception
           int n = Integer.parseInt("One");
       }
       catch(NumberFormatException ex)
       {
           //Handling and printing exception message
           System.out.println(ex.toString());
       }
      
       //String Exception
       try
       {
           //Sample String
           String s = "Hello World";
           //Trying to fetch a character from string with invalid index value - Generates Exception
           char ch = s.charAt(100);
       }
       catch(StringIndexOutOfBoundsException ex)
       {
           //Handling and printing exception message
           System.out.println(ex.toString());
       }
      
       //User Defined Exceptions
       try
       {
           //Throwing Exception
           throw new UserException("User Defined Exception Generated.");
       }
       catch(UserException ex)
       {
           //Handling and printing exception message
           System.out.println(ex.toString());
       }
   }
}

_________________________________________________________________________________________

Sample Run:


Related Solutions

JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This...
JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This time, define classes ExceptionA (which inherits from class Exception) and ExceptionB (which inherits from class ExceptionA). In your program, create try blocks that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and IOException. All exceptions should be caught with catch blocks specifying type Exception.
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string and number exception handling, as well as a user defined exception CREATE A NEW PROGRAM that demonstrates Any 2 of the following 3 choices: 1 numeric and 1 string exception make the program require 1 input argument and throw an exception if there are no command line arguments passed in. In the exception, inform the user that an input argument is required. (You can...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available...
Create a program that accepts in a string of 2 or more words. The program then...
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should...
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command...
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command line and prints every permutation of that string. You may assume the string will contain all unique characters. You may print the permutations in any order, as long as you print them all. ---------------- Output: ---------------- $>./prog dog d do dog dg dgo o od odg og ogd g gd gdo go god ---------------- I need help on this exercise that requires recursion only....
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command...
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command line and prints every permutation of that string. You may assume the string will contain all unique characters. You may print the permutations in any order, as long as you print them all. ---------------- Output: ---------------- $>./prog dog d do dog dg dgo o od odg og ogd g gd gdo go god ---------------- I have no idea on coding this. The files I...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you run the program first time for example it will print 3 then when you run the program for the second time it will give a random number for example 9 but number 7 will not be printed because we need you to do exception for it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT