Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Inside my {Try} I attempt to load a file [streamReader], if this fails (for example the file is not there) the error is caught in the {Catch} and the function terminates [exits {Try}].

 

Problem is I do not want the application to stop because it was unable to load the file, is there a way to catch the error [inside the {Catch} I would log the error to file] and then return to the {Try} section to continue processing?

Edited by Shaitan00
  • *Gurus*
Posted

In most cases a Try block should only contain one or two statements. However more often than not one comes across blocks with multiple lines of code shoved into them, which is a poor practice at best.

 

The steadfast rule is: keep the number of lines to a minimum, and the exceptions specific.

 

What should you do if code that follows the Try/Catch block shouldn't execute if an exception was thrown? Change a boolean variable in the Catch block and use If/Then logic to detect the outcome.

Posted
Problem is I do not want the application to stop because it was unable to load the file, is there a way to catch the error [inside the {Catch} I would log the error to file] and then return to the {Try} section to continue processing?

 

There is no continue statement that acts like a loop. However you can create your own loop;

 

bool processing = true;

while (processing) { ... }

 

In your try block, set processing to false (as you're obviously done if the code succeeds). In your catch block, set processing to false (an error occured, try again).

Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...