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(;

                        String username,password;

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

                        username = in1.nextLine();

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

                        password = in1.nextLine();




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


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




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



                        catch(LoginException ex)    // Own Exception







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.



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

Earn money online

1049total visits,3visits today


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.


Link back if you want.

Enter Captcha Here :*

Reload Image