【问题标题】:Return in try catch statement in java在java中的try catch语句中返回
【发布时间】:2014-08-25 10:35:01
【问题描述】:

在这个方法中,我可以在哪里使用return语句?终于进去了?还是试试?

在 try catch 语句中返回字符串我有点困惑。

这是我的代码。

   public List<String> getEmailAddr(String strAccountnbr, String strCode) throws Exception {

        String strQuery2 = null;
        ResultSet rs = null;
        PreparedStatement ps = null;

     try

     {
        List<String> emailAddress= new ArrayList<String>();
        strQuery2 =  "SELECT c.EmailAddress AS EmailAddress" +

            " FROM customeremailid c " +
            "WHERE c.AccountNbr = ? " +
            "AND c.Code = ? ";

          logMsg("strQuery2: "+strQuery2);

          ps = getDBConn().prepareStatement(strQuery2);
          ps.setString(1, strAccountnbr);   
          ps.setString(2, strCode); 
          rs = ps.executeQuery();

        while(rs.next())
        {   
            emailAddress.add((rs.getString("EmailAddress")));   
        } 


     }

     catch(Exception e)
     {
         e.printStackTrace();
     }  

     return emailAddress;
     }

我收到emailAddress cannot resolved to a variable. 的错误 有什么帮助吗?

【问题讨论】:

  • 肯定不在finally 块内。

标签: java return try-catch finally


【解决方案1】:

return可以用在try块内,也可以用在try - catch块外,finally块基本上是为了放弃资源而不是写return语句。

【讨论】:

  • 不,这不是真的。您可以将此 try-catch 限制为仅处理可能的查询异常,然后处理 try/catch 之外的其余代码(包括返回语句)。
  • @Handsomeguy 如果我没听错,你的意思是说没有必要写finally块?
  • 不,不正确的是“必须始终在 try 块内使用返回”
  • @Handsomeguy 删除了模棱两可的部分,因为还有另一行 "or can be used outside try-catch block.
【解决方案2】:
public List<String> getEmailAddr(String strAccountnbr, String strCode) throws Exception {

        String strQuery2 = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        List<String> emailAddress= new ArrayList<String>();

     try{

         strQuery2 =  "SELECT c.EmailAddress AS EmailAddress" +
                      " FROM customeremailid c " +
                      " WHERE c.AccountNbr = ? " +
                      " AND c.Code = ? ";

          logMsg("strQuery2: "+strQuery2);
          ps = getDBConn().prepareStatement(strQuery2);
          ps.setString(1, strAccountnbr);   
          ps.setString(2, strCode); 
          rs = ps.executeQuery();

        while(rs.next())
        {   
            emailAddress.add((rs.getString("EmailAddress")));   
        } 


     }

     catch(Exception e)
     {
         //some error especify your exception or uses a generic..
         throw new EmailAdressException("BLA BLA BLA...")
     }  
     //if everything is fine return  email
     return emailAddress;
     }

【讨论】:

    【解决方案3】:

    您需要考虑范围。在您的代码中,当您在一个块(try-block)中声明您的 emailAddress 时,emailAddress 仅在该块内可用。 try-catch 在这里仅用于处理执行查询时可能出现的异常。尝试将 try-catch 限制为仅会引发异常的代码(在这种情况下,您可以/应该重构代码,以便在其自己的方法中完成查询)。

    如果您在 try-catch 块之外声明 emailAddress,则在 catch 块之后返回它。

    public List<String> getEmailAddr(String strAccountnbr, String strCode) throws Exception {
        List<String> emailAddress= new ArrayList<String>();
    
         try {
             // Handle query here
         } catch(Exception e) {
             // You should not catch Exception, but a more fine-grained exception.
             // In this case, SQLException
         } 
    
         return emailAddress;
     }
    

    【讨论】:

      【解决方案4】:

      将您的代码更改为

      List<String> emailAddress= new ArrayList<String>();
      try
      {
      ....
      }
      
      return emailAddress;
      

      以前,您尝试返回的变量仅限于 try 块范围。

      【讨论】:

        【解决方案5】:

        我收到错误,因为 emailAddress 无法解析为变量。任何 帮忙?

        List<String> emailAddress= new ArrayList<String>();//Declare it outside the block
        try{...
        

        OR 在外部声明但在try 内部初始化,以使其在catch 块中可访问。

        List<String> emailAddress=null;
        try{...
        emailAddress= new ArrayList<String>();//Initialize it inside the block
        

        现在您的emailAddress 只能在try{//Block} 中访问,而不能在catch{//Block} 中访问

        【讨论】:

        • 我在块外声明。如果没有电子邮件地址,我是否会在 catch 中收到异常?
        • @user3152748 你会得到 Nullpointer 但如果你得到任何异常你可以在 catch 块中返回 null 或特定的错误消息。这是你的赌注! :)
        猜你喜欢
        • 1970-01-01
        • 2012-04-24
        • 2012-11-19
        • 2018-10-14
        • 2021-12-25
        • 2014-03-10
        • 1970-01-01
        • 2011-07-20
        • 2018-05-23
        相关资源
        最近更新 更多