Custom Exceptions using throw keyword

Maximum MNC company create own structure code. In this structure code they also create Custom Exceptions handling classes, methods and constructors.

In this blog example, we create LoginException exception for check login details of admin panel.

 

LoginException : Custom Exceptions

import java.util.*;                  

class LoginException extends Exception

{

            LoginException(String errorMessage)

            {

                        super(errorMessage);           // Pass this message to Exception Class

            }

}                      

class Exc8

{

            public static void main(String args[])

            {

                        Scanner in1 = new Scanner(System.in);

                        String username,password;

                        System.out.print(“Enter UserName : “);

                        username = in1.nextLine();

                        System.out.print(“Enter PassWord : “);

                        password = in1.nextLine();

 

                        try

                        {

                 if(username.compareTo(“admin”) == 0 && password.compareTo(“12345”) == 0)

                                    {

                                                System.out.println(“Welcome in Invision Software Solution”);

                                    }

                                    else

                                    {

                                                throw new LoginException(“Invalid Username or Password”);

                                    }

                        }

                        catch(LoginException ex)    // Own Exception

                        {

                                    System.out.println(ex);        

                        }

            }

}          

Output:

Output 1: [Perfect username and password]

Enter UserName: admin

Enter Password: 12345

Welcome in Invision Software Solution

 

Output 2: [Invalid Username and password]

Enter UserName: admin

Enter Password: 1234

LoginException: Invalid Username or Password

 

Video available on YouTube now.

 


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


Earn money online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

783total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint