增强try catch代码 [英] enhancing try catch code
问题描述
using(Transaction acTrans = acDB.TransactionManager.StartTransaction())
{
try
{
// request for要在绘图区域中选择的对象
PromptSelectionResult selection = editor.GetSelection(selectionFilter);
如果提示状态为OK,则选择onjects
if(selection.Status!= PromptStatus.OK)
{
editor.WriteMessage(" wtf there)是没有有效的选择");
acTrans.Dispose();
}
SelectionSet selecionSet = selection.Value;
//检查以确保返回了有效的SelectedObject对象。
if(selecionSet == null)
{
editor.WriteMessage(" wtf that was No selection");
acTrans.Dispose();
}
//结束交易
acTrans.Commit();
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
editor.WriteMessage(ex.Message);
}
最后
{
acTrans.Dispose();
}
}
是代码使用try catch correctrly?
你不需要"终于"阻止 - 这就是"使用"确实。
"try / catch"虽然很好。
或者,你可以留下"终于"。阻止并删除"使用"一部分。
using (Transaction acTrans = acDB.TransactionManager.StartTransaction()) { try { // request for object to be selected in the drawing area PromptSelectionResult selection = editor.GetSelection(selectionFilter); if the prompt status is OK, onjects were selected if (selection.Status != PromptStatus.OK ) { editor.WriteMessage("wtf there was No valid selection "); acTrans.Dispose(); } SelectionSet selecionSet = selection.Value; // Check to make sure a valid SelectedObject object was returned. if (selecionSet == null) { editor.WriteMessage("wtf there was No selection "); acTrans.Dispose(); } // end the transaction acTrans.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { editor.WriteMessage(ex.Message); } finally { acTrans.Dispose(); } }
is that code uses try catch correctrly?
You don't need the "finally" block - that's what the "using" does.
The "try/catch" is fine though.
Alternatively, you could leave the "finally" block in and remove the "using" part.
这篇关于增强try catch代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!