发生错误后如何使try / catch继续工作?

解析器代码可用


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”?


SMILET
浏览 270回答 6
6回答

qq_笑_17

一种方法是try catch在您的ParseAll方法中添加: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", "");但这一切都取决于程序要求流程将如何进行。

PIPIONE

您可以缩小 try-catch 块:解析器代码可用// May need its own try-catch blcokid_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 blcokposition = await ParsingAll(0, "div.title.adv-title.newTitle > h1", "");

MMTTMM

这样做的唯一方法是将行拆分为单独的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 - ");    }…

一只萌萌小番薯

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

慕田峪4524236

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

慕森王

使用 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", "");}
打开App,查看更多内容
随时随地看视频慕课网APP