Error handling with FIJI Macro (instead of crashing)

I know the imagej language is limited (I haven’t had the chance to get into Jython yet, unfortunately, it’s next on my quarantine list of things to learn!), but there is a macro I have to finish by tomorrow - it is iterating through 3D stacks in a folder, doing some analysis, saving results, pretty standard.
It will be used by other people so I wonder where I can read about error handling in ImageJ macro, i.e. instead of opening the debug window, which the user will never understand, and crash, to give a meaningful error, and the macro either to continue as normal, or to stop working.
For example, if the image that has been opened (by the macro) is not in the required format (TIFF, 3D stack), it should just continue on to the next image (and notify the user of this) instead of crashing.
Say the user has to draw a ROI in the image, if they do not draw it, prompt the user to do this again.
I am aware of the if/else statements + print() option, but then print() would just print my message in the log window, and the programme will still crash. So when the macro is restarted, the same thing will happen again.
Is it possible to do this with the FIJI macro? if so, where can I read about it? Apologies if I haven’t made this clear enough.

Hi @Isq,

error handling in ImageJ macro basically means checking if results are present or if image have the right format before an error can happen. That’s good practice in programming anyway.

In such a case you can work with if () blocks to check if the image has the right dimensions and format. E.g.

getDimensions(width, height, channels, slices, frames);
if (slices == 1) {
	print("error: the image is not 3D");
} else {
	// process the image
}

Hm, not sure if I understand. If the crashing code is inside the else-block, why is it executed, if the error message shows up? Can you maybe share example code?

If usually check this case by checking if the current ROI size corresponds to the size of the image:

getDimensions(width, height, channels, slices, frames);

waitForUser("Please draw an ROI");

Roi.getBounds(x, y, roiwidth, roiheight);

if (roiwidth == width && roiheight == height) {
	print("Error: no ROI selected");
}

Last but not least, you can try this operation: But it’s really the last backup solution if you cannot prevent/catch an error before happening:

If you have a more specific situation we maybe can give you more detailed hints, e.g. what error may actually happening?

I hope this helps!

Cheers,
Robert

1 Like

Hi @haesleinhuepf, Robert, thank you loads for the helpful answer!
Yes what you say makes sense, it is just that the code is quite long with several components to be checked, so I wanted to avoid having everything in layers “else” brackets. (Having said that I am a pretty newbie programmer, so I might be thinking about it wrong)…
Is there any equivalent to “continue” or “break” like in other programming languages? Like :
if (some errorenous condition) {print(error message)}; else {continue}
So the macro comes back to the beginning of the function and continues iterating through the other images in the folder?
I will try to extract a suitable chunk of code soon. Thanks!

1 Like

Yes! They both exist and can be used in loops.

For exiting functions, you can use the return command.

If you feel your code becomes long and messy, you can read about the “Divide and rule” concept. For example in this ppt starting at slide 32:

Cheers,
Robert

1 Like

Ah of course, they are only for loops. Thanks for slides too, when I am done with this project I will take a look at this too.
I love the image analysis/development community here is so supportive :slight_smile:

1 Like