【问题标题】:How to make try / catch continue to work after an error?发生错误后如何使try / catch继续工作?
【发布时间】:2019-01-06 08:12:53
【问题描述】:

解析器代码可用

try
{
   id_source = await ParsingAll(0, "#adv_id", "");   
   foto_path = await ParsingAll(1, "img[id='print_user_photo']", "src");
   position = await ParsingAll(0, "div.title.adv-title.newTitle > h1", "");
catch (Exception ex)
{
    Error?.Invoke(id_source + "- Error - ");    
}   

如果字符串“foto_path”出现错误怎么办,然后处理try/catch错误后,程序继续工作,开始执行字符串“position”?

【问题讨论】:

  • 嗯,解决方案取决于了解您对架构的要求是否可以接受在 ParsingAll 方法中添加 try catch inside。如果是,那么这是一个简单有效的解决方案

标签: c# exception exception-handling try-catch


【解决方案1】:

一种方法是在 ParseAll 方法中添加 try catch

ParsingAll()
{
   try
   {

   }
   catch(Exception e)
   {
   }
}

你可以正常调用它们:

id_source = await ParsingAll(0, "#adv_id", "");   
foto_path = await ParsingAll(1, "img[id='print_user_photo']", "src");
position = await ParsingAll(0, "div.title.adv-title.newTitle > h1", "");

并返回一些带有结果的状态以判断它是否成功。

或者您需要将它们分别包装起来,以便在该语句失败时执行下一条语句:

try
{
    foto_path = await ParsingAll(1, "img[id='print_user_photo']", "src");
}
catch(Exception e)
{
}

position = await ParsingAll(0, "div.title.adv-title.newTitle > h1", "");

但这一切都取决于程序要求流程将如何进行。

【讨论】:

    【解决方案2】:

    这样做的唯一方法是将行拆分为单独的try...catch 子句:

    try
    {
       id_source = await ParsingAll(0, "#adv_id", "");   
    catch (Exception ex)
    {
        Error?.Invoke(id_source + "- Error - ");    
    }
    try
    {
       foto_path = await ParsingAll(1, "img[id='print_user_photo']", "src"); 
    catch (Exception ex)
    {
        Error?.Invoke(id_source + "- Error - ");    
    }
    …
    

    【讨论】:

      【解决方案3】:

      你可以缩小 try-catch 块:

      解析器代码可用

      // May need its own try-catch blcok
      id_source = await ParsingAll(0, "#adv_id", "");
      
      try
      {   
          foto_path = await ParsingAll(1, "img[id='print_user_photo']", "src");
      catch (Exception ex)
      {
          Error?.Invoke(id_source + "- Error - ");    
      }
      
      // May need its own try-catch blcok
      position = await ParsingAll(0, "div.title.adv-title.newTitle > h1", "");
      

      【讨论】:

        【解决方案4】:

        从例程中获取 foto_path 值,而不是 Try catch 或者 将 try catch 放入 ParsingAll 例程中。

        【讨论】:

          【解决方案5】:

          您可以考虑在 async ParsingAll 方法中捕获错误并仅返回该方法的有效输出。

          【讨论】:

            【解决方案6】:

            如何使用 finally 块,无论是否发生异常,它都将始终执行。我认为这更像是一种解决方法,但最好的解决方案应该是根据您的情况在 ParsingAll() 方法中处理它。

            try
            {
               id_source = await ParsingAll(0, "#adv_id", "");   
               foto_path = await ParsingAll(1, "img[id='print_user_photo']", "src");
            }
            catch (Exception ex)
            {
                Error?.Invoke(id_source + "- Error - ");    
            }
            finally
            {
                position = await ParsingAll(0, "div.title.adv-title.newTitle > h1", "");
            }
            

            【讨论】:

              猜你喜欢
              • 2021-01-22
              • 2019-06-19
              • 1970-01-01
              • 2013-11-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-04
              • 1970-01-01
              相关资源
              最近更新 更多