Java 我可以使用多个try-catch块抛出多个错误或异常吗

Java 我可以使用多个try-catch块抛出多个错误或异常吗,java,maven,api,bitbucket,jgit,Java,Maven,Api,Bitbucket,Jgit,下面的代码是通过java的git push命令,如果我不使用try-catch-block运行,它会成功地推送文件,但我不知道如果用户使用try-catch-block输入错误的url、用户名和密码,如何抛出错误。任何人都可以对代码进行正确的编辑 public void pushRepo (String repoUrl, String gitdirectory, String username, String password) throws GitAPIException {

下面的代码是通过java的git push命令,如果我不使用try-catch-block运行,它会成功地推送文件,但我不知道如果用户使用try-catch-block输入错误的url、用户名和密码,如何抛出错误。任何人都可以对代码进行正确的编辑

  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");
      }

    }
}

如果我输入的url、用户名和密码的值不正确,它总是会给出类似“无效用户名和密码”的消息。

尝试创建自定义验证器

public class ValidationErrorException extends Exception {

private List<String> errors;

public ValidationErrorException(List<String> errors) {
    super("Validation Errors.");
    setErrors(errors);
}

public ValidationErrorException(String message, List<String> errors) {
    super(message);
    setErrors(errors);
}

public void setErrors(List<String> errors) {
    this.errors = errors;
}

public List<String> getErrors() {
    return errors;
  }
 }
公共类ValidationErrorException扩展异常{
私有列表错误;
公共ValidationErrorException(列出错误){
超级(“验证错误”);
设置错误(错误);
}
公共ValidationErrorException(字符串消息,列表错误){
超级(信息);
设置错误(错误);
}
公共void setErrors(列表错误){
这个。错误=错误;
}
公共列表getErrors(){
返回错误;
}
}
加入你的函数

抛出ValidationErrorException

然后使用

try {
        // write code for check url
    } catch (Exception ex) {
        List<String> errors = new ArrayList<>();
        errors.add("Invalid URL...");
        throw new ValidationErrorException(errors);
    }
试试看{
//编写检查url的代码
}捕获(例外情况除外){
列表错误=新建ArrayList();
错误。添加(“无效URL…”);
抛出新的ValidationErrorException(错误);
}

用户名和密码等

尝试创建自定义验证程序

public class ValidationErrorException extends Exception {

private List<String> errors;

public ValidationErrorException(List<String> errors) {
    super("Validation Errors.");
    setErrors(errors);
}

public ValidationErrorException(String message, List<String> errors) {
    super(message);
    setErrors(errors);
}

public void setErrors(List<String> errors) {
    this.errors = errors;
}

public List<String> getErrors() {
    return errors;
  }
 }
公共类ValidationErrorException扩展异常{
私有列表错误;
公共ValidationErrorException(列出错误){
超级(“验证错误”);
设置错误(错误);
}
公共ValidationErrorException(字符串消息,列表错误){
超级(信息);
设置错误(错误);
}
公共void setErrors(列表错误){
这个。错误=错误;
}
公共列表getErrors(){
返回错误;
}
}
加入你的函数

抛出ValidationErrorException

然后使用

try {
        // write code for check url
    } catch (Exception ex) {
        List<String> errors = new ArrayList<>();
        errors.add("Invalid URL...");
        throw new ValidationErrorException(errors);
    }
试试看{
//编写检查url的代码
}捕获(例外情况除外){
列表错误=新建ArrayList();
错误。添加(“无效URL…”);
抛出新的ValidationErrorException(错误);
}

用户名和密码等

最好的方法是创建一个自定义验证器并抛出它。关键问题的可能重复是:为什么您希望用户多次(每次提交时)配置Git远程URL和凭据@MincongHuang我想提供Validation实现这一点的最佳方法是创建一个自定义验证器并将其抛出。关键问题可能是:为什么您希望用户多次(每次提交时)配置Git远程URL和凭据@MincongHuang我想提供验证非常感谢您的ans先生,我知道这将真正帮助我解决问题,但请您在我的代码中进行编辑。因为我无法理解我以前从未使用过它,我们这个社区不应该这样做,但在聊天中找到我,我会为您这样做,因为这可以帮助很多其他人,不仅是你的代码非常感谢你的ans先生,我知道这真的可以帮助我解决问题,但是你可以在我的代码中进行编辑吗,请。因为我无法理解我以前从未使用过它,我们这个社区不应该这样做,但是在聊天中找到我,我会为你做的,因为这可以帮助很多其他人,不仅仅是你的代码