tavalendo
tavalendo

Reputation: 877

Catch part of Try-Catch not executing

I am writing a shortcode for learning purpose that asks the user to enter a password to login to Facebook. I am testing exception handling, and for some reason, the Catch part is not executing when the password is wrong. The code is:

import java.util.Scanner;

public class FacebookLogin {
    
    
    public static void printPassword() {
        Scanner sc = new Scanner(System.in);
        String password;
        
        try {
            
            System.out.print("Enter your password : ");
            password = sc.next();
        
        } catch (Exception x) {
            do {            
            System.out.println("Your password is incorrect. Try again!");
            System.out.print("Enter your password : ");
            sc.nextLine();
            password = sc.next();
            
            
            } while (password != "password");
        }
        sc.close();
        
    }
    
    public static void main(String[] args) {
        
        
        System.out.println("Welcome to Facebook!");
        
        printPassword();
        
        System.out.println("Congratulations, you have logged in to Facebook!");
        
        
    }
    
}

Few runs of the above script:

Welcome to Facebook!

Enter your password : ksskasjaks

Congratulations, you have logged in to Facebook!

Another run:

Welcome to Facebook!

Enter your password : password

Congratulations, you have logged in to Facebook!

I excepted something like this, of course the only password here is "password":

Welcome to Facebook!

Enter your password : ksskasjaks

Your password is incorrect. Try again!

Enter your password : password

Congratulations, you have logged in to Facebook!

Any clue why it is not working as intended? Thanks.

Upvotes: 0

Views: 98

Answers (2)

Willem
Willem

Reputation: 1060

With try catch:

 public static void enterPassword() throws Exception {
    Scanner sc = new Scanner(System.in);
    String password;
    System.out.print("Enter your password : ");
    password = sc.next();
    if (!password.equals("password")) {
        throw new Exception();
    }
}

public static void printPassword() {
    try {
        enterPassword();
    } catch (Exception e) {
        System.out.println("Your password is incorrect. Try again!");
        printPassword();
    }
}

public static void main(String[] args) {


    System.out.println("Welcome to Facebook!");

    printPassword();

    System.out.println("Congratulations, you have logged in to Facebook!");


}

Upvotes: 1

Willem
Willem

Reputation: 1060

No need for try catch, i guess this is what you wanted:

public static void printPassword() {
    Scanner sc = new Scanner(System.in);
    String password;

    System.out.print("Enter your password : ");
    sc.nextLine();
    password = sc.next();
    while (!password.equals("password")) {
        System.out.println("Your password is incorrect. Try again!");
        System.out.print("Enter your password : ");
        sc.nextLine();
        password = sc.next();
    }

    sc.close();

}

public static void main(String[] args) {


    System.out.println("Welcome to Facebook!");

    printPassword();

    System.out.println("Congratulations, you have logged in to Facebook!");


}

Upvotes: 0

Related Questions