MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: mathlink error handling inside :Begin: - :End:

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109581] Re: mathlink error handling inside :Begin: - :End:
  • From: John Fultz <jfultz at wolfram.com>
  • Date: Thu, 6 May 2010 04:52:05 -0400 (EDT)

There isn't a problem with your usage of AbsoluteFileName[], but it's not going
to help you handle errors.  By the time anything in :Arguments: is handled, it's
too late...stuff will be sent to your MathLink program regardless.

There are a few ways you could handle this in Mathematica.

* Create a wrapper function.  The wrapper function should detect the error
condition and never call the template-defined function until the arguments have
been validated.

* Be more subtle about how you declare your patterns.  You could declare the
pattern in the function here as...

    :Pattern: ReadEXR[ s_String?FileExistsQ ]

This isn't very good for error-handling (will just return unevaluated in error
cases), but you can cleverly handle errors by defining a *second* function
purely in Mathematica as so...

    ReadEXR[s_] :== (* error implementation *)

* Handle the error on C++.  To give a hint to the MathLink program that you have
an error, you could add an extra argument...

:Arguments: { s, FileExistsQ[s] }
:ArgumentTypes: { String, Symbol }

Absolutely nothing in the template specification enforces that the number of
arguments be the same between the pattern declaration and the arguments
declaration, so you can generate as much metadata from the Mathematica inputs as
you like.

Sincerely,

John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.


On Wed, 5 May 2010 06:05:18 -0400 (EDT), olanilsson wrote:
> Hi,
>
> I have a small question about the mathlink-workings (I have gone
> through the documentation but can't seem to find an answer). I have an
> external mathlink/c++ program with the following signature:
>
> :Begin:
> :Function:      readexr
> :Pattern:       ReadEXR[ s_String ]
> :Arguments:     { AbsoluteFileName[s] }
> :ArgumentTypes: { String }
> :ReturnType:    Manual
> :End:
>
> :Evaluate: ReadEXR::usage == "ReadEXR[fn] reads an OpenEXR file and
> return its rgba channels."
>
> I thought I was clever when I used AbsoluteFileName, but I can't seem
> to detect any file-not-exist error. If I try to open a non-existent
> file a message is send to the kernel and printed in the frontend, but
> no error is found if I probe for MLError in my C++-code. Thus, my
> questions are:
>
> 1. Could I detect such an error already inside the Begin-End block,
> and if so abort execution at this point? Is this approach a good idea?
> 2. If not, how can (or should) I detect the error-state in the C++-
> code?
>
> Regards,
>
> Ola


  • Prev by Date: Re: Plot modular function on unit disk
  • Next by Date: Re: Optimization problem for dice game (repost)
  • Previous by thread: Re: mathlink error handling inside :Begin: - :End:
  • Next by thread: Re: mathlink error handling inside :Begin: - :End: