how to validate a sales invoice or a JV

i am working on autopost function to post transactions

for JV i use the codeunit “Gen. Jnl.-Post” to post a JV

and the codeunit “Sales-Post” to post a sales invoice,

if there are a uncorrect JV or sales invoice the system give me an error and it exit the form ,

HOW I CAN SKIP AND COUNT THE INCORRECT JV AND SALES INVOICE

thanks in advance