6 回答
TA贡献1818条经验 获得超7个赞
一种方法是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", "");
但这一切都取决于程序要求流程将如何进行。
TA贡献1829条经验 获得超9个赞
您可以缩小 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", "");
TA贡献1869条经验 获得超4个赞
这样做的唯一方法是将行拆分为单独的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 - ");
}
…
TA贡献1777条经验 获得超3个赞
使用 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", "");
}
- 6 回答
- 0 关注
- 223 浏览
添加回答
举报