A middle-aged customer in Bass Pro was in his boat cabin, somewhere in the Caribbean...when he heard footsteps up on deck.
There was nothing he could do except get robbed. That country doesn't allow visitors to keep guns on boats. But having the right gun--legal or not-- might Not be the real issue here.
A Different guy in the Caribbean was in a very similar situation and hearing feet on the deck, went up the ladder with a gun in his hand. The thugs on deck immed. shot and killed the boat owner (a pro. bicycle racer), before the boat owner could even assess the situation, see where they were. He had little common sense, but might have had a very Cool Gun.
One can never assume that a robber has No tactical advantage. They probably use tiny motors (or battery-powered?) on skiffs when approaching targeted boats late at night. If I were a ruthless thug, I would take one other guy, blacken my face, arms, and quietly paddle to a target at 0400 on a moonless night.
Don't assume that you are smarter, more clever than an experienced, sociopathic criminal with initiative, just because you are of higher moral character than a drug gang, have more integrity, have more education and
might be better-looking.
If a gun (in your hands, on a slippery deck) is the solution to all of these potential issues, try out rental guns at a range and don't kid yourself about what you like. But chances are very good that any salty mist or air will corrode it somehow and knock a big chunk from the resale value.