• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

pinging without exceptions

I use IdIcmpClient1.Ping, wich works great. But I don't want the program to give an error when the host is unavial.
0
Stream12
Asked:
Stream12
1 Solution
 
DaFoxCommented:
Hi.

What about try..except?

try
  ...
  IdIcmpClient1.Ping;
except
end;

Markus
0
 
swift99Commented:
if you do this then you should handle the exception to determine whether or not it is one that you really want to ignore.  As written, this can hide some nasty bugs.
0
 
smurffCommented:
remember that if you put it in the Try..Except you will get a message within the delphi IDE but you will not see it when it is compiled.

So to sum up : Even if you put it in a Try..Except, the IDE will show an exception error but when its compiled, it will not be there and it will just run your code below the "Except" section


Regards
Smurff
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
steve_hskCommented:
Hi Stream ...

If you're happy using a third party component, that is designed to throw it's own exception type, then this is perfectly valid, and as the guys suggested a simple try .. except statement around the function that causes the exception would work just fine.

Alternatively, you could write your own component using the ICMP.DLL, or use RAW SOCKETS which we can provide examples for.

TRY ... EXCEPT EXAMPLE :

// Try executing code that may raise exceptions
TRY
   // Attempt to Ping the client
   IdIcmpClient1.Ping(...);
   EXCEPT // An exception has been raised
   BEGIN
      // Handle this exception
      label1.caption := 'Ping Failiure : ...';
   END;
END;

Alternatively if there is a specific exception that is raised, it is progammatically better to only catch this specific exception, eg EPingViolation :

// Try executing code that may raise exceptions
TRY
   // Attempt to Ping the client
   IdIcmpClient1.Ping(...);
   EXCEPT ON EPingViolation DO
   BEGIN
      // Handle this exception
      label1.caption := 'Ping Failiure : ...';
   END;
END;

Just for interest, as Smurf was saying about the exceptions being raised in the Delphi IDE, even though you have used a try ... except :

This can be prevented by going TOOLS >> DEBUGGER OPTIONS >> LANGUAGE EXCEPTIONS >> ADD ... "EPingViolation"

Delphi will now ignore this exception/error if you use the try ... except statement around the offending code.

Hope this helps,
Steve
0
 
smurffCommented:
Steve,

>This can be prevented by going TOOLS >> DEBUGGER OPTIONS >> LANGUAGE EXCEPTIONS >> ADD ... "EPingViolation"

Didnt know that thanks ;)

Another point to add about indys ICMP component is that you have to be an administrator user. Not sure why but if this causes you a problem then as Steve said use the ICMP dll. Examples can be found here or at other tips sites such as www.delphi3000.com , www.torry.net etc etc

Regards
Smurff
0
 
Stream12Author Commented:
Thanks to all.

I didn't know any of the things you guys told me.

I'm gonna try that .dll thing.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now