解析器代码可用
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”?
答案 0 :(得分:3)
一种方法是在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", "");
但这一切都取决于程序要求,流程将如何进行。
答案 1 :(得分: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 - ");
}
…
答案 2 :(得分:2)
您可以缩小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", "");
答案 3 :(得分:1)
从例程中获取foto_path值,而不是Try catch 要么 将try catch放入ParsingAll例程中。
答案 4 :(得分:1)
您可以考虑在异步ParsingAll方法中捕获错误,并仅从该方法返回有效输出。
答案 5 :(得分:1)
如何使用无论发生异常如何都将始终执行的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", "");
}