A 40 or a 45 will kill a bear, but probably not before he has done whatever it was he was going to do to you. Their hide and fur are MUCH thicker than a human. A typical hollow point bullet one would be firing from a 40 would not expand in a bear the same way it would on a human.
I used to hunt a lot. Never hunted for bear but have a number of buddies who do. One friend hit one square in the lungs with a 30-06 rifle. The bear flinched, looked at him and continued ambling along on its merry way. 3 solid hits from that rifle later, the bear dropped. Another buddie hit one square with a 44mag that he had loaded VERY hot (max charge listed in the reloading books). This guy is a very experienced hunter and a good shot. He tracked the bear for over 3/4 mile before he found it dead.
Both of these firearms are far more powerful than any calibre one would typically carry as a self defence weapon. Given those experiences, I wouldn't consider a handgun anything more than a last stand weapon for a bear attack. I suppose they would be better than kicking and punching but I wouldn't count on it to drop an angry bear in its tracks.
Pepper spray would most likely have a more immediate effect in knocking the wind out of his sails and one wouldn't have to worry about where the bullet landed should he/she miss.
I still compete in target shooting events and from time to time serve as a range officer but I don't hunt any more and, thank the Lord, I've never fired a weapon at another person and pray that I never have to.
Sounds like you may have (or still) are a police officer or military person. If that is the case, thank you for your service. I appreciate the job you do (or have done).