本文介绍了尝试... Catch ..结束尝试或如果...结束IF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! HI, 我正在处理我的游戏循环,当处理图形设备表格时,我得到''ObjectDisposedException''。我把绘制后缓冲区的代码放到了''try ... end try''这样的语句中的目标表面: 尝试 _bbg = 我 .CreateGraphics _bbg.DrawImageUnscaled(_bb, 0 , 0 ) Catch ex As ObjectDisposedException 结束 尝试 这解决了问题,但后来我记得我在某个地方读过 ''尝试''陈述很重,我我们决定用一个简单的布尔表达式替换它们 ,该表达式检查表格是否要处理或不处理: 如果 booKill = False 那么 _bbg = 我 .CreateGraphics _bbg.DrawImageUnscaled(_bb, 0 , 0 ) 结束 如果 这也是没有抛出异常的伎俩,但现在我想知道 这两个例子中哪一个更快? div class =h2_lin>解决方案 我想说两者都是因为booKill是boolean并且并不能保证关于_bb的任何内容。除非你无法做任何事情来解决它,因为那时try..catch大部分时间都没用。 如果booKill = False然后尝试 _bbg = Me.CreateGraphics _bbg.DrawImageUnscaled(_bb,0,0) Catch ... 修复代码... 结束尝试结束如果 祝你好运! 你很困惑防御性编程(如果booKill = False)带异常处理(尝试... Catch )... Me.CreateGraphics可能仍会抛出一个异常,但只有布尔检查你就不会捕获它。 但要回答你关于最快的问题...试着写一个使用这两种方法处理大量卷的小型测试程序......简单来说,布尔检查始终执行,而Try-Catch处理只有在有异常的情况下才会被调用,这意味着后者会更快。我怀疑你会不会注意到它们之间的区别。 在明确发生了什么的情况下,写下防守和正确处理异常然后我会建议接近EF Nijboer的解决方案 HI,I''m working on my game loop and when disposing the graphics device formI get ''ObjectDisposedException''. I put the code that draws the back buffer tothe target surface in ''try... end try'' statements like this:Try _bbg = Me.CreateGraphics _bbg.DrawImageUnscaled(_bb, 0, 0)Catch ex As ObjectDisposedExceptionEnd Tryand this solved the problem, but then I remember that I read somewherethat ''try'' statements are heavy and I''ve decided to replace themwith a simple boolean expression that checks whether the form is about tobe disposed or not:If booKill = False Then _bbg = Me.CreateGraphics _bbg.DrawImageUnscaled(_bb, 0, 0)End IfThat also do the trick without throwing exceptions, but now I''m wonderingwhich one of the both examples is faster? 解决方案 I would say both because booKill is boolean and doesn''t really guarantee anything about _bb. Unless you cannot do anything to fix it, because then a try..catch is most of the time just useless.If booKill = False Then Try _bbg = Me.CreateGraphics _bbg.DrawImageUnscaled(_bb, 0, 0) Catch ... Fix code... End TryEnd IfGood luck!You''re confusing defensive programming (If booKill = False) with exception handling (Try ... Catch) ... Me.CreateGraphics might still throw an exception but with just the boolean check you wouldn''t capture it.But to answer your question about "fastest" ... try writing a small test program to process large volumes using both methods ... In simplistic terms, the boolean check is always executed whereas the Try-Catch processing only gets invoked if there''s an exception, which implies that the latter would be "faster". I doubt you''ll really notice the difference.In terms of making it clear what''s going on, writing defensively and handling exceptions properly then I would suggest something close to E.F. Nijboer''s solution 这篇关于尝试... Catch ..结束尝试或如果...结束IF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 14:17