If you really care about that, use the a gate held open by an animal using a "dead man's switch".
One simple way to do this, is in your entrance corridor, this is a cross section (P = pressure plate, d = dog or other animal, ___ = bridge, \ = ramp)
################
d ___#####
###P####\
################
Make the pressure plate triggered by friendlies, and pasture the animal on top of it in a 1x1 pasture.
When enemies arrive, they will either kill the animal or it will move by itself. The bridge will then return to it's default state, which, as a retracting bridge, blocks traffic going up or down the ramp. A retracting bridge used in this way can't be jammed open - there is no possible way, it also wont squash anything. This trap is also trifling to extend to make a self-sealing chamber - just have another retracting bridge over the exterior entrance. The chamber can then be flooded (I would link that to a lever, rather than the pressure plate!)
The one and only problem with this method, is the enemy might stand on the pressure plate and re-open the gate and something might slip through before the gate re-closes. This risk can be almost entirely eliminated by putting the pressure plate somewhere enemies wont path, for example at the outside of a bend in the hallway.
This of course does nothing to address the problem of dorfs being stranded outside, which is a problem regardless of whether or not you have a sealable entrance. I suggest taking your most important dwarves (such as legendary armorsmiths) and turning off all extraneous labors such as hauling, recovering wounded, and cleaning. This way only useless immigrants will be caught outside. If you want to be hardcore, have your barracks on the outside (rookes should train deep inside where it's safe though). Then the self-sealing entrance provides a useful fallback for if your military loses.