A 40 gallon tank is a nice size. Some salt water fish are short lived. I have had fire fish, Bengai cardinals and gobies die of apparent old age. Other species can, with good care, live for 20 plus years. My tangs seem to be immortal.
Marine fish are not that hard to keep but they have certain needs. Avoid swings in water conditions. They come from the ocean which is an extremely stable environment. Get an auto top off unit to maintain constant salinity. Keep the temperature reasonably constant (a shift of a couple degrees per day is OK). Maintain the tank. Keep the critical chemical factors in bounds.
Also, for a 40 gallon tank, you want fish that do well in small systems which leaves out quite a few things.
Oh, and give an adequate diet which varies with the fish. It is not that hard: my fish get nori, frozen mysis, frozen enriched brine and a mix of 4 kinds of pellets.
And try not to introduce diseases which means quarantine everything before tossing into the tank.
If you give the fish what they need, deaths should be a pretty rare event.
Good luck to you.