我有一個代碼從web服務獲取Product
對象。如果沒有產品,則返回EntityDoesNotExist
異常。我需要處理這個..但是,我有很多其他代碼處理返回Product
,但如果此代碼不在try/catch中,它不起作用,因爲Product
基本上未定義。是否只有這樣做才能在try/catch中包含我的其他相關代碼?這似乎很sl。。使用try/catch使得我不能使用try/catch塊外的變量
代碼示例:
try {
Product product = catalogContext.GetProduct("CatalogName", "ProductId");
} catch(EntityDoesNotExist e) {
// Do something here
}
if(dataGridView1.InvokeRequired) {
// Do something in another thread with product
}
你如何訪問'product'外的try-catch的_declared_ ** **在它:這可以通過移動代碼內的
try
塊做什麼? –