using try/except with grass.mapcalc in a Python Script

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

using try/except with grass.mapcalc in a Python Script

Jenny Turner
Greetings

I want to include grass.mapcalc function in try except but I'm not catching the errors. Has anyone used this for grass.mapcalc?

Thanks
Jenny

_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: using try/except with grass.mapcalc in a Python Script

Glynn Clements

Jenny Turner wrote:

> I want to include grass.mapcalc function in try except but I'm not catching
> the errors. Has anyone used this for grass.mapcalc?

What sort of error are you expecting?

If you call set_raise_on_error(), a non-zero exit status should raise
a ScriptException instead of calling sys.exit() (note that sys.exit()
raises a SystemExit exception, which can be caught if desired).

Note that r.mapcalc will only return a non-zero exit status for
fundamental errors such as syntax errors in the expression or failure
to open a map. Calculation errors (e.g. division by zero or domain
errors) result in null values.

--
Glynn Clements <[hidden email]>
_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: using try/except with grass.mapcalc in a Python Script

Jenny Turner
Hello Glynn

What I want to do is run grass.mapcalc like this:
try:
   grass.mapcalc()
except:
   grass.fatal(_("An error occurred ..."))

I know that this doesn't work. What I want to catch is not "syntax" erros but errors such as an raster input map that does not exist and is being used in grass.mapcal should raise an error or an excpetion in order to stop the process.

Any sugfgestions?

Thanks
Jenny

On Tue, Mar 1, 2011 at 7:26 PM, Glynn Clements <[hidden email]> wrote:

Jenny Turner wrote:

> I want to include grass.mapcalc function in try except but I'm not catching
> the errors. Has anyone used this for grass.mapcalc?

What sort of error are you expecting?

If you call set_raise_on_error(), a non-zero exit status should raise
a ScriptException instead of calling sys.exit() (note that sys.exit()
raises a SystemExit exception, which can be caught if desired).

Note that r.mapcalc will only return a non-zero exit status for
fundamental errors such as syntax errors in the expression or failure
to open a map. Calculation errors (e.g. division by zero or domain
errors) result in null values.

--
Glynn Clements <[hidden email]>


_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: using try/except with grass.mapcalc in a Python Script

Glynn Clements

Jenny Turner wrote:

> What I want to do is run grass.mapcalc like this:
> try:
>    grass.mapcalc()
> except:
>    grass.fatal(_("An error occurred ..."))

grass.mapcalc() already calls grass.fatal() if r.mapcalc exits with a
non-zero exit code (which will happen in the case of a syntax error or
if G_fatal_error() is called by r.mapcalc itself or by any GRASS
library function).

A bare "except" will catch any exception, including the SystemExit
exception which sys.exit() generates. If your handler isn't being
invoked, that suggests that r.mapcalc isn't reporting an error.

Normally, grass.fatal() calls sys.exit() which terminates the process.
Calling grass.set_raise_on_error() will cause grass.fatal() to raise a
ScriptException instead (but if you catch it, remember to call
grass.set_raise_on_error(False) to restore the original behaviour of
grass.fatal()).

> I know that this doesn't work. What I want to catch is not "syntax" erros
> but errors such as an raster input map that does not exist and is being used
> in grass.mapcal should raise an error or an excpetion in order to stop the
> process.

The grass.mapcalc() function only knows the exit status from
r.mapcalc. It cannot distinguish between different types of error.

--
Glynn Clements <[hidden email]>
_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user