异常处理try catch catch [英] Exception handling try catch inside catch

查看:148
本文介绍了异常处理try catch catch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个程序员编写的代码,他在一个catch里面有一个try catch语句。



请原谅我无法粘贴实际的代码,但他所做的是类似于这个

  try 
{
// ABC操作

catch(ArgumentException ae)
{
try
{
// XYZ操作
}
catch(IndexOutOfRangeException ioe)
{
//某事
}
}

我个人觉得这是我见过的最穷的代码之一!
在1到10的范围内,你认为我应该多快去给他一个想法?
或者我过度反应?



编辑:
他实际上在做的是在执行中,他正在执行一些其他操作初始尝试失败时可以/应该完成。我的问题是有一个干净的代码和可维护性。将异常从第一个catch指定给另一个函数或调用函数将可以,但是添加更多的代码可能会抛出异常到第一个catch,这是我感觉不是很好。我尽量避免多次堆叠的if-loop语句,我发现这同样糟糕。




  • IvarD


解决方案

为什么那么糟糕?在概念上不同于:

  void TrySomething(){
try {


} catch(ArgumentException){
HandleTrySomethingFailure();
}
}

void HandleTrySomethingFailure(){
try {

} catch(IndexOutOfRangeException){

}
}

在你过去之前给他一块你的大脑(尝试顶叶,这是特别令人反感),你究竟要对他说什么?你会如何回答这个谚语为什么?



更讽刺的是,当抖动引入这段代码时,它将看起来就像你的例子。

-Oisin


I recently came across a code written by a fellow programmer in which he had a try catch statement inside a catch!

Please forgive my inability to paste the actual code, but what he did was something similar to this

try
{
 //ABC Operation
}
catch (ArgumentException ae)
{
   try
   {
      //XYZ Operation
   }
   catch (IndexOutOfRangeException ioe)
   {
      //Something
   }
}

I personally feel that it is one of the poorest code I have ever seen! On a scale of 1 to 10...how soon do you think I should go and give him a piece of my mind? Or am I over-reacting?

EDIT: What he is actually doing is that in the catch, he is performing some other operations which can/should be done when the initial try fails. My problem is with having a clean code and maintainability. Delegating the exception from the first catch to a different function or the calling function would be OK, but adding more code which may or may not throw an exception into the first catch, is what I felt was not good. I try to avoid multiple stacked "if-loop" statements, I found this equally bad.

  • IvarD

解决方案

Why is that bad? It's no different conceptually than:

void TrySomething() {
   try {


   } catch (ArgumentException) {
        HandleTrySomethingFailure();
   }
}

void HandleTrySomethingFailure() {
    try {

    } catch (IndexOutOfRangeException) {

    }
}

Before you go over there and give him a piece of your brain (try the parietal lobe, it's particularly offensive) , what exactly are you going to say to him? How will you answer the proverbial "why?"

What's even more ironic is that when the jitter inlines this code, it will look exactly like your example.

-Oisin

这篇关于异常处理try catch catch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆