In: Computer Science
C# Create an application that asks the user to enter their new password. The password must be at least 6 characters long. Develop a custom exception handling case that checks for the password length. (hint: use " .Length " built-in method). If the new password is less than 6 characters long the custom exception should output an error message.
SOLUTION-
I have solve the problem in C# code with comments and screenshot
for easy understanding :)
CODE-
using System;
namespace ConsoleApp // Project name
{
class Program
{
class InvalidPasswordException : Exception
{
public InvalidPasswordException()
: base(String.Format("Invalid Passwrod, Password length should be
greater then 6 letters"))
{
}
}
class Password
{
static void Main(string[] args)
{
while (true)
{
try
{
// read input
Console.Write("Enter password must be at least 6 characters long:
");
string pass = Console.ReadLine();
// check password length
if (validatePassword(pass))
{
Console.WriteLine("Done.");
break;
}
}
catch (InvalidPasswordException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Try again.");
}
}
Console.ReadKey();
}
private static bool validatePassword(string pass)
{
// if password length is less then 6
if (pass.Length < 6)
{
// throw exception
throw new InvalidPasswordException();
}
return true;
}
}
}
}
Output:
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------