Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Below code is of git push command through java everything if I run without using try catch block it successfully pushing the files, But im not getting that how to throw error if user enter wrong url and username and password by using try and catch block can anyone do correct edit in the code for it.

public void pushRepo (String repoUrl, String gitdirectory, String username, String password) throws GitAPIException
  {

        Git git = null;
      try {
          git = Git.open(new File(gitdirectory));
      } catch (IOException e1) {
          System.out.println("it is not git directory");
      }
        RemoteAddCommand remoteAddCommand = git.remoteAdd();
        remoteAddCommand.setName("origin");
        try {
        remoteAddCommand.setUri(new URIish(repoUrl));
        System.out.println("file added");
        }catch (Exception e) {
             System.out.println("Invalid RemoteUrl");
          }
        remoteAddCommand.call();
        git.add().addFilepattern(".").call();
        git.commit().setMessage("commited").call();
        PushCommand pushCommand = git.push();
        try {
        pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
        pushCommand.setRemote("origin").add("master").call();
        System.out.println("push file");
        }catch(Exception e)
        {
            System.out.println("Invalid username and password");
        }

      }
  }


What I have tried:

If I enter value incorrect of any of url ,username and password it always give the message like "invalid username and password."
Posted
Comments
Richard MacCutchan 25-Mar-18 3:53am    
You do not generally use try/catch to validate user input. Get the entered values, validate them, and if there is a mistake then tell the user to try again. But also, you are printing generic messages in your catch blocks, rather than the details of the exception.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900