Try - catch

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

Is there a way to check if there are no exceptions?
I now use the understanding code, are there other options?

bool noExceptions = true;
try { }
catch { noExceptions = false; }

if (noExceptions == true) { }

Thanks!
 
Arjen,

That's pretty much the way you would do it. Normally though, I wouldn't
recommend swallowing the exception like that unless you had to.

Hope this helps.
 
Arjen,

Your code:

bool noExceptions = true;
try {
// CODE BLOCK 1
}
catch { noExceptions = false; }

if (noExceptions == true) {
// CODE BLOCK 2
}

Could be rewritten as follows:

try {
// CODE BLOCK 1
// CODE BLOCK 2 - happens only if no exceptions in block 1 !!
}
catch {
}

(no variable needed). But then as Nicholas said, instead of "swallowing" the
exception, generally is better to simply write:

// CODE BLOCK 1
// CODE BLOCK 2

(no try-catch at all) and let the calling context to handle the possible
exception.

Regards - Octavio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top