2

Parser code is available

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 - "); } 

How to do if an error occurs in the string "foto_path", then after processing the try / catch error, the program continued to work and began to execute the string "position"?

1
  • 1
    Well the solution depends on whether knowing your requirements for architecture it is acceptable to add try catch inside of ParsingAll method. If yes then it's a simple and effective solution Commented Jan 6, 2019 at 9:18

6 Answers 6

3

One way is that to add try catch inside your ParseAll method :

ParsingAll() { try { } catch(Exception e) { } } 

and you can call them normally:

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", ""); 

and return some status with result to tell if it was successful or not.

Or you will need to wrap it separately each of them so that the next statements should execute in case of that one fails:

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", ""); 

But this all depends on the program requirements how the flow will go.

Sign up to request clarification or add additional context in comments.

Comments

2

The only way to do so is to split the lines into separate try...catch clauses:

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 - "); } … 

Comments

2

You could just narrow the try-catch block:

Parser code is available

// 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", ""); 

Comments

1

Get foto_path value from a routine, than has Try catch Or put try catch in the ParsingAll routine.

Comments

1

You may consider to catch the error within the async ParsingAll method and return only a valid output from that method.

Comments

1

How about using the finally block which will always execute irrespective of exception occurred. I consider this more of a workaround but best solution should be handle it in ParsingAll() method for your situation.

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", ""); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.