C# Program: Convert string to uppercase with exception handling
C# Sharp Exception Handling: Exercise-11 with Solution
Write a C# program that creates a method that takes a string as input and converts it to uppercase. Handle the NullReferenceException that occurs if the input string is null.
Sample Solution:
C# Sharp Code:
using System;
class Program {
static void Main() {
try {
// Ask the user to input a string
Console.Write("Input a string: ");
string input = Console.ReadLine();
// Convert the input string to uppercase
string result = ConvertToUppercase(input);
// Display the converted uppercase string
Console.WriteLine("Uppercase string: " + result);
} catch (NullReferenceException) {
// Catch block for handling NullReferenceException (null input)
Console.WriteLine("Error: Input string is null.");
} catch (ArgumentException ex) {
// Catch block for handling ArgumentException (null or empty input)
Console.WriteLine("Error: " + ex.Message);
} catch (Exception ex) {
// Catch block for handling other types of exceptions
Console.WriteLine("An error occurred: " + ex.Message);
}
}
// Method to convert a string to uppercase
static string ConvertToUppercase(string input) {
// Check if the input string is null or empty
if (string.IsNullOrEmpty(input)) {
// Throw an ArgumentException if the input is null or empty
throw new ArgumentException("Input string is null or empty.");
}
// Convert the input string to uppercase using the ToUpper method
return input.ToUpper();
}
}
Sample Output:
Input a string: Error: Input string is null or empty.
Input a string: java Uppercase string: JAVA
Input a string: Java Uppercase string: JAVA
Explanation:
In the above exercise -
- Inside the "Main()" method, a try block is used to enclose code that may throw exceptions.
- The program prompts the user to input a number using Console.Write() to display the message and Console.ReadLine to read the input as a string.
- The input is converted to double using Convert.ToDouble. If the input is a valid number, it is stored in the number variable.
- An if statement checks if the number is greater than or equal to 0. If it is, the program calls the CalculateSquareRoot method to calculate the square root of the number.
- If the number is negative, an ArgumentException is thrown with the message "Number cannot be negative!" using the throw keyword.
- The catch block follows the try block to handle specific exceptions.
- The first catch block catches a FormatException, which occurs if the user enters invalid input that cannot be converted to a double. In this case, it displays an error message: "Error: Invalid input. Please input a valid number."
- The second catch block catches an ArgumentException, which is thrown when the number is negative. It displays the error message contained within the exception.
- The last catch block catches any other exceptions and displays a generic error message along with the exception message.
- The CalculateSquareRoot method takes a double parameter number and uses the Math.Sqrt method to calculate the square root of the number. It returns the result.
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Calculate square root with exception handling.
Next: Calculate factorial with overflow exception handling.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/exception-handling/csharp-exception-handling-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics