Question

In: Computer Science

This question has been answered already however, I'd like some new examples. Task: In C#, create...

This question has been answered already however, I'd like some new examples.

Task: In C#, create a minimum of three try/catch statements that would handle potential input errors. Thank you.

Solutions

Expert Solution

Taking 3 try-catch for 3 exceptions:

1) NotaPositiveNumberException

2) InvalidUserNameException

3) IndexOutOfRangeException

Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryCatchDemo
{
class NotaPositiveNumberException : ApplicationException
{
public NotaPositiveNumberException(string str) : base(str)
{ }
}

class InvalidUserNameException : ApplicationException
{
public InvalidUserNameException(string str) : base(str)
{ }
}
class Program
{
static void Main(string[] args)
{
//1. try..catch
//NotaPositiveNumberException
int number;
try
{
Console.WriteLine("---------------------------------------------------");
Console.Write("Enter a positive number: ");
number = Convert.ToInt32(Console.ReadLine());
if (number < 0)
{
throw new NotaPositiveNumberException("InvalidNumber");
}
else
{
Console.WriteLine("The entered number is a positive number!");
}
}
catch (NotaPositiveNumberException n1)
{
Console.WriteLine("The number is not a positive number!!");
}
Console.WriteLine("---------------------------------------------------");

//2. try..catch
//InvalidUserName - Enter a name that should not be greater than 15 characters
string userName;
try
{
Console.Write("\nEnter an username(less than 15 characters): ");
userName = Console.ReadLine();
if (userName.Length > 15)
{
throw new InvalidUserNameException("InvalidUserName");
}
else
{
Console.WriteLine("\nValid Username!!");
}
}
catch (InvalidUserNameException ie)
{
Console.WriteLine("\nThe length of a username should not be greater than 15 characters!");
}
Console.WriteLine("---------------------------------------------------");
//3. try..catch
//IndexOutOFBoundException - Array
//Enter the index to show the element
int[] arr = new int[5];
int index;
  
  
//enter 5 elements
  
Console.WriteLine("\n\nEnter 5 elements: ");
for (int i = 0; i < 5; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
try
{
Console.WriteLine("Enter the index to show the element: ");
index = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The element is "+arr[index]);
}
catch (IndexOutOfRangeException ie)
{
Console.WriteLine("\nInvalid index number!!");
Console.WriteLine("\nArray index out of bound exception!");
}
Console.WriteLine("---------------------------------------------------");
Console.ReadKey();
}
}
}

Output:


Related Solutions

******I KNOW THIS QUESTION HAS ALREADY BEEN ANSWERED; HOWEVER I WOULD LIKE A DIFFERENT ANSWER***** With...
******I KNOW THIS QUESTION HAS ALREADY BEEN ANSWERED; HOWEVER I WOULD LIKE A DIFFERENT ANSWER***** With the aid of supply and demand diagrams, explain how each of the following would likely affect the prices and quantities of cigarettes sold in the US. (a) A cure is found for lung cancer. (b) There is a substantial increase in wages in tobacco growing states. (c) A fertilizer that increases the yield per acre of tobacco is discovered. (d) There is a substantial...
**New code needed! Please do not reference code that has already been answered for this question...
**New code needed! Please do not reference code that has already been answered for this question as that code contains errors*** Write a C++ program to simulate a service desk. This service desk should be able to service customers that can have one of three different priorities (high, medium, and low). The duration for any customer is a random number (between 5 minutes and 8 minutes). You need to write a program that will do the following: Generate random 100...
This question has already been answered, but there was not as much detail as i needed...
This question has already been answered, but there was not as much detail as i needed to understand what exactly to do and i need to understand it quick... could you please walk me through how to do this, but specifically A and B please plug in all numbers and state where they came from , thank you so much! Calculate the potential in the following cell Pb|Pb2+(1.00M)|| Ag+(1.00M) |Ag a. right at the beginning when you start using this...
Please answer with a new answer not one that has already been answered on here before....
Please answer with a new answer not one that has already been answered on here before. Many supervisors are not well-trained on the difficult task of terminating an employee and instead resort to other methods of forcing someone out of the organization. Methods include giving the employee unpleasant work tasks, reducing their hours, or modifying their jobs in some negative way. What are the ethical issues raised by this strategy and what are the risks to the organization?
Question 1 has already been answered. Please answer question 2 below the table only. Find out...
Question 1 has already been answered. Please answer question 2 below the table only. Find out the NPV, IRR and Payback period for each of the following three projects. Assume I=6%.                                                                                                                                    (60 points) Project CF0 CF1 CF2 CF3 CF4 CF5 A -12,000 3000 3000 4000 4000 1000 B -12,000 4000 4000 3000 3000 1000 C -12,000 3000 3000 3000 3000 1000 D -12,000 5000 3000 5000 3000 0 E -12,000 3000 3000 3000 3000 5000 F...
PLEASE ANSWER C&D ONLY . THE REST HAS ALREADY BEEN ANSWERED. Say the marginal tax rate...
PLEASE ANSWER C&D ONLY . THE REST HAS ALREADY BEEN ANSWERED. Say the marginal tax rate is 20 percent and that government expenditures do not change with output. Say also that the economy is at potential output and that the deficit is $450 billion.     a. What is the size of the cyclical deficit? Answer = $0   b. What is the size of the structural deficit?    Answer = $450 billion.    c. How would your answers to a and...
This problem has already been half answered, but can someone finish it and check the answers....
This problem has already been half answered, but can someone finish it and check the answers. I'll include the link. Finished ASAP please! https://www.chegg.com/homework-help/questions-and-answers/use-weighted-average-costing-wanda-company-produces-finished-product-two-processing-depart-q29753062 USE WEIGHTED AVERAGE COSTING: Wanda Company produces its finished product in two processing departments--Mixing and Finishing. The following information is available for the month of March: Mixing Department: The beginning work-in-process inventory was $17,130 ($14,880 direct materials and $2,250 conversion costs) and consisted of 1,200 units. During March, an additional 10,600 units were started into production....
This question has been answered before. I need a new, slightly modified code for the following:...
This question has been answered before. I need a new, slightly modified code for the following: A palindrome is a word that it reads the same left to right and right to left. For this programming assignment, you need to write a C++ program that does the following: Request the user to enter a string. Write a recursive function that will test the string to validate if it is a palindrome or not. Respond back with an output saying if...
The whole question has been posted below, but I have answered some of them. I need...
The whole question has been posted below, but I have answered some of them. I need help with questions B, E, and F. I need the critical value in question B. I need the answers for the differences in question E. I need the answers for test statistics in F. Thank you. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Answer the one-way ANOVA questions using the data below. Use α = 0.01. 1 2 3 4 53 49 47 42 48 34 44 44 39 36...
Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with...
Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with a few classes. Requirements: Automated Teller Machine (ATM) simulationGiven 3 trials, the user is able to see his balance by entering a four-digit pin that must NEVER be displayed on screen but masked by the Asterix (*) character. A list of pins stored on the file system must be loaded to verify the pin. The user should be able to withdrawfundsbelow a set limit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT