curl: catching exception on connect.

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 1 00:57:46 PST 2014


On 11/30/2014 10:38 PM, Suliman wrote:

 > Am I right understand all exception are derived from "assertThrown"
 > http://dlang.org/phobos/std_exception.html

No, all exceptions are derived from Throwable. It has two descendants:

- Error, representing conditions that are irrecoverable, and

- Exception, representing conditions that are recoverable.

Those two have many descendants themselves.

assertThrown() is useful in unit testing. It is a function that checks 
whether an expression indeed throws an exception.

 > So "msg" in "catch(Exception msg)"

'catch' catches an exception object. You should name it as 'exc' or 
something similar. 'msg' makes it sound like "message" which it is not.

 > is from function "assertThrown"?
 >
 > Could you show me example of how to handle type of exception?

Your example works: You are actually handling the exception. What you 
did with it later was confusing to you and to us.

The following program realizes that a connect() call failed by catching 
an exception. It simply outputs a message about it.

import std.stdio;
import std.net.curl;

pragma(lib, "curl");

const links = [ "ddili.org/non_existent_link" ];

void downloadFile()
{
     foreach(link; links)
     {
         try
         {
             connect(link);

         } catch(Exception exc) {
             import std.string;
             writeln(format("Failed to download '%s'.", link));
         }
     }
}

void main()
{
     downloadFile();
}

Ali



More information about the Digitalmars-d-learn mailing list