Stream12
asked on
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
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
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
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
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
>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
ASKER
Thanks to all.
I didn't know any of the things you guys told me.
I'm gonna try that .dll thing.
I didn't know any of the things you guys told me.
I'm gonna try that .dll thing.