Should Quit problem

I want to ask a user to do something when quitting an app, so I throw a display dialog statement in “should quit” handler and make it return true/false based on the returned reply. The dialog has 3 buttons: “Don’t save”, “Cancel” and “Save”. Only when “Save” is hit the app terminates normally. Clicking on the other two buttons gave me this exception:

Modal session cannot be run during application termination

I have also noticed that “Cancel” button has different semantics (obviously it quits an app on the spot). I’d like to ask what is the code pattern for doing this seemingly simple task. Thank you.

This works for me:

on should quit theObject
	try
		set x to button returned of (display dialog "Quit?" buttons {"Don't save", "Cancel", "Save"} default button 3)
	on error number -128 --> catch "Cancel" button
		return false
	end try
	if x = "Save" then delay 3 --> save or whatever
	return true
end should quit

What code are you using exactly?