简体   繁体   中英

Continue Automatically After Exception

So I am writing an academic software where I need to get data from a network of 8 devices via online links. Note that this devices are configured in such a fashion that they sometime return no or null data. And I need to collect this data for a long time. Here, is the code:

public static void ParseJsonStatic(string link, ...9)
{
    /access the URLs in a suitable interval and process data
    var client = new WebClient();
    var stream = client.OpenRead(link);
    Debug.Assert(stream != null, "stream != null");
    var reader = new StreamReader(stream);
    var rootObject = JsonConvert.DeserializeObject<RootObject>(reader.ReadToEnd());

    ....
}

So whenever there is a null stream , Visual Studio will pause and show me the exception bubble and I will have to click this continue button.

在此处输入图片说明

Is there a way to handle this and make sure my code continues to run from start if such a situation occurs. So what I want is this:

while (stream == null) { ... retry to read stream and don't trigger nullPointerException... }

Because pausing in middle fails my purpose of data collection based on specific interval and also I cannot leave it unattended as such for long intervals.

Thanks.

Try this:

var stream = null;
while (stream == null) {
    stream = client.OpenRead(link)
}

Maybe between reads you also want to wait for some time.

In Visual Studio: Ctrl + Alt + E or Debug > Exceptions.

This will open up a window where you can select which expections will cause the debugger to break.

The ones you are intrested in will probably be under "Common Language Runtime Exceptions"

Note that any unhandled exception which would cause your app to crash will still break debugging. You need to make sure to handle the exception with a Try/Catch.

Go to the Debug menu and then the Exceptions

To access this window go to the Debug menu and select Windows -> Exception Settings.

Select add and type in your exception. This will add a checkbox item for your exception.

Anytime that exception is thrown your debugger will pause if that checkbox is on. In your situation this checkbox must be unchecked.

See this link on msdn for more info!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM