It's not exactly trial and error and there is no fixed rule.
Even when the fingering is noted in the sheet you sometimes find that you have to alter it because it doesn't fit your hand. All hands are different and what applies to one may not necessarily apply to other.
As a few pointers.Try to keep the thumb away from black keys unless needed.If you play 3 black keys in a row use 4-3-2 so you can use 1 in the following white (or 2-3-4 in the opposite direction).If there are 2 black keys use 3-2-1.
You should also generally use the fingering that demands less movement out of your hands so you can play at a faster speed when needed.If you make unecessary movements you may have trouble when you try to raise the speed.
I sometimes must change fingering to playing faster, because when it come to play slow, any fingering can be use without plroblem....