C#(C Sharp) Login Program Using Exception Handling

C Sharp -  Login Program(Exception Handling)


Here in this article I have created a program in c sharp(c#) for login using exception handling, In Addition to that I have created userdefined exception which is derived from the in-build class ApplicationException, and by deriving this userdefined exception I am creating two different exception for userID and Password, which throws exceptions if any occurred while entering user Id and password or even if the user details is not available in the database.

Read Also :  Do you have Antivirus Installed ? Check whether its working or not.

Program Coding:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Login
{
    class LoginException : ApplicationException
    {
        public LoginException()
        {
        }
        public LoginException(string str) : base(str)
        {
        }

    }
    class InvalidUserIDException : LoginException
    {
        public InvalidUserIDException():base()
        {
            Console.WriteLine("Invalid UserID Exception Occured");
        }

    }
    class InvalidPasswordException : LoginException
    {
        public InvalidPasswordException(string Message) : base(Message)
        {
            Console.WriteLine("Invalid Password Exception Occured");
        }

    }
Advertisement---------------------------------------------------------------------
-----------------------------------------------------------------------------------End of Advertisement
    class Login
    {
        public int UserID { get; set; }
        public string Password { get; set; }
        public bool Authentication(int UID, string Password) // to check true or false
        {
            bool result = false;
            Dictionary<int, string> userdetails = new Dictionary<int, string>();
            userdetails.Add(1, "John123");
            userdetails.Add(2, "Jessica23");
            userdetails.Add(3, "Monica");
            userdetails.Add(4, "Francis");
            userdetails.Add(5, "Tom");
            string str = null;
            bool check = userdetails.TryGetValue(UserID,out str);
            if(!check)
            {
                throw new InvalidUserIDException();
            }
            else
            {
                if(str==Password)
                {
                    result =  true;
                }
                else
                {
                    throw new InvalidPasswordException("Invalid Password");
                }
            }
            return result;           
        }

    }
   
    class Program
    {
        static void Main(string[] args)
        {
            try
            {               //Outer Exception
                try
                {           //Inner Exception
                    Login lg = new Login();
                    Console.WriteLine("#####---Login Page----######");
                    Console.WriteLine("");
                    Console.WriteLine("Enter the user ID");
                    lg.UserID = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter the Password");
                    lg.Password = (Console.ReadLine());
                    bool registered = lg.Authentication(lg.UserID, lg.Password);
                    Console.WriteLine("The Entered", +lg.UserID + "and" + lg.Password + "Password are" + registered);
                }
                catch (FormatException)
                {
                    throw new LoginException("Invalid Login Details Entered");
                }
                finally
                {
                    Console.WriteLine("Session Ends");
                }
            }
            catch (LoginException)
            {
                Console.WriteLine("Login Error");
            }
            Console.ReadLine();
        }
    }
}
Output :


Hope you understand the logic, method and syntax used in this program, do comment below if you have any queries related to this program, we will immediately respond to your queries with solutions.

No comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...
 

Do not Copy this Web Page Content

Copyrighted.com Registered & Protected  0K43-TWEU-OGOE-SNU2 DMCA.com Protection Status
The content displayed on this website are copyright protected, reproducing the article in any kind is strictly prohibited.
Copyright © . How to Comp - Instant Solution for Computer and Mobile Problems | All Rights Reserved.
Designed By Way2themes | True Tech Tips | Teentack