[GRASS-user] using try/except with grass.mapcalc in a Python
Script
Glynn Clements
glynn at gclements.plus.com
Fri Mar 11 12:39:21 EST 2011
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 <glynn at gclements.plus.com>
More information about the grass-user
mailing list