I must caveat my answer with the disclaimer that I am not an attorney or a bankruptcy specialist. You should consult with an attorney if you are facing either option.
All else equal, if an underwater mortgage is your principal liability, foreclosure is better for two reasons: (1) in many states you can "hand back the keys" of your property to the bank without impacting your other assets. In other words, the lender will not not seek recourse beyond the property securing the original loan. While this is not a desirable outcome, it is better than having the lender or a bankruptcy trustee come after all of your personal assets. An attorney can give you more specifics for your state and particular circumstances. In addition, there may be solutions short of foreclosure that are open to you ~ the federal government has been expanding its programs to keep people in their homes; perhaps there are other options available.
(2) While both foreclosure and bankruptcy would adversely affect your credit rating and ability to obtain financing in the future, lenders have told me that the effect of foreclosure is more limited than bankruptcy. Again, an attorney can give you more specifics. Good luck!