# Is Net Available

Posted by Sean de Wolski,

Sean‘s pick this week is isnetavl by Rik Wisselink.

I was recently writing a function that downloads data from a public location online. I had to call it multiple times to get
data for different time ranges but wanted to safe guard against there being no internet connection. If the system failed
to download data, I wanted to be able to add the internet connectivity information to the exception.

Rik’s isnetavl function, which is an enhancement to another version without Unix support, does this.

This is roughly what the code I ended up with looks like. To run this, my computer is in airplane mode.

try
% Loop over dates, storing output for each one.
for ii = numel(dates):-1:1
end
catch ME
failedquery = MException('DataReader:FailedQuery', 'url: %s\ndate: %s', url, char(dates(ii)));

% rethrowing the exception.
if ~isnetavl
nointernet = MException('System:NoInternetConnection', 'The system is not connected to the internet.');
end

% Rethrow the exception with additional causes
rethrow(ME)

end
Error using webread (line 122)
The IP address of "www.mathworks.com" could not be determined.
Error in mainisnetavl (line 28)
Caused by:
url: https://www.mathworks.com
date: 04-Jul-2017 13:00:00
The system is not connected to the internet

Caution: Though this function tells you the internet connectivity of your computer, it may not reflect the internet connectivity of
MATLAB. MATLAB may have its own proxy settings that interfere with its access to the internet.

Is connectivity of MATLAB to the internet something you would like to have built-in, perhaps as part of the HTTP Interface? If so, please provide your use case here.

Give it a try and let us know what you think here or leave a comment for Rik.

Get the MATLAB code

Published with MATLAB® R2018a

2 views (last 30 days)  | |