c#中的错误处理 [英] Error Handling in c#

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

问题描述





任何人都可以告诉我如何处理程序中的错误调用C#中的其他程序



在下面的例子中,我是否使用了try catch块。



如果在被调用的程序中发生任何错误,我需要停止并退出P1 。



如果我们有

 私人  void  P1()
{
P2();
P3();
....
}

private void P3()
{
P4();
...
...

}

private void P4()
{
}





谢谢

Nirmala

解决方案

在方法P1中添加错误处理程序。

例如

< pre lang =cs> private void P1()
{
< span class =code-keyword> try
{
P2();
P3();
....
}
Catch(例外情况)
{
// 处理程序
}
}



P2,P3或P4中的任何errpr将翻转到父级handler。


这在很大程度上取决于那些函数中发生了什么,想要处理错误,如果你只想记录错误和转到下一行/事务/无论如何。如果你没有处理(例如)P4中的错误,那么错误将被抛出到调用函数,如果你不在那里处理它将会调用那个调用那个的函数,依此类推。



以下链接中有一些关于错误处理最佳实践的文章



http://msdn.microsoft.com/en-us/library/seyhszts(v=vs.110) .aspx [ ^ ]

异常处理最佳.NET中的实践 [ ^ ]

http://codebuild.blogspot.co.uk/2012/01 /15-best-practices-about-exception.html [ ^ ]



刚才有另一个想法。我们经常在QA中看到代码中的代码

 尝试 
{
P1();
}
catch (例外)
{
// 什么都不做
}



这比没有任何错误处理更糟糕只是吞下,你永远不会知道你的程序是否有效。不要这样做


如果你没有做任何错误处理,P2(),P3()或P4()中发生的任何错误都会传播给调用者堆。因此,如果您只是想退出,如果有任何错误,您不必在此示例中做任何额外的事情。



但是,我建议在main方法中添加某种日志记录或其他异常处理(假设在这种情况下为P1),以便稍后在调试或故障排除时为您提供详细信息。



因此:



  private   void  P1()
{
try
{
P2();
P3();
}
catch (异常例外)
{
// 记录或处理此异常
}
}

private void P3()
{
P4();
}

private void P4()
{
}


hi,

Can anybody tell me how to handle errors inside procedure which calls other procedures in C#

In the following example, were do i use try catch block.

I need to stop and exit from P1 if any error occurs any were in the called procedures.

if we have

private void P1()
{
P2();
P3();
....
}

private void P3() 
{
P4();
...
...

}

private void P4()
{
}



Thanks
Nirmala

解决方案

Add error handlers in method P1.
For e.g.

private void P1()
{
  try
  {
    P2();
    P3();
  ....
  }
  Catch (Exception ex)
  {
    //Handler
  }
}


Any errpr in P2, P3 or P4 will rollover to the parent handler.


It very much depends on what is happening in those function, where you want to handle the errors, and if you just want to log the error and move on to the next row/transaction/whatever. If you don't handle an error in (say) P4 then the error will be thrown up to the calling function, if you don't handle it there it will go up to the function calling that one and so on.

There are some articles on best practices for error handling on the following links

http://msdn.microsoft.com/en-us/library/seyhszts(v=vs.110).aspx[^]
Exception Handling Best Practices in .NET[^]
http://codebuild.blogspot.co.uk/2012/01/15-best-practices-about-exception.html[^]

[EDIT] Just had another thought. We often see code in QA here along the lines of

try
{
    P1();
}
catch (Exception)
{
    //do nothing
}


This is worse than not having any error handling as the exception is just "swallowed" and you will never know whether your program worked. Don't do this


If you don't do any error handling, any error which occurs in P2(), P3() or P4() would be propagated to the caller in the stack. Thus, in case of any error if you just want to exit, you don't have to do any thing extra in this example.

However, I would suggest to add some sort of logging or other exception handling in the main method (assuming P1 in this case) so that it can give you details later while debugging or trouble shooting.

Thus:

private void P1()
{
    try
    {
        P2();
        P3();
    }
    catch(Exception exception)
    {
        //Log or handle this exception
    }
}

private void P3()
{
    P4();
}

private void P4()
{
}


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

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