NullReferenceException handling in in-line asp.net code
问题 I have some inline code in an aspx file that executes: <dd><%= encode(Listing.Address.Line1) %> </dd> Problem is, under certain circumstances the Listing object will be null and therefore references to properties such as Address will throw an exception. How do I handle that exception? I basically want to ignore it: catch it, and then proceed with regular execution/rendering of page. 回答1: Do a null check. It is almost always better to test for the conditions of an exception that you expect