try catch块异常hanlding [英] exception hanlding by try catch block

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

问题描述

我已经阅读了各种文章抛出和处理异常通常是一项昂贵的操作。我已经审查了我组织中许多开发人员的代码,发现大多数开发人员用户使用此类型代码如下:



I have read various article throwing and handling an exception is usually an expensive operation. i have reviewed code of many developer in my organisation and found that most of developer user this type code is given below

private void RunnSqlLoader()
    {
        string sp1 = _strPath + _strFileName + ".bat";

        try
        {

            ProcessStartInfo startInfo = new ProcessStartInfo(@sp1)
            {
                WorkingDirectory = _strPath,
                CreateNoWindow = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false
            };
            Process batchExecute = new Process { StartInfo = startInfo };
            batchExecute.Start();
            batchExecute.WaitForExit();
        }
        catch (Exception ex)
        {
            throw ex;
        }


    }





所以此代码是否使用更好的异常处理?

或我的观点在这里没用。但我不确定?





请有人解释我



so Is this code use better exception handling ?
or my point of view its not useful here. but i am not sure about?


please anybody explain me

推荐答案

该代码是异常处理,但相当无意义的处理。如果您正在做的只是重新抛出异常,那么不要打扰try\catch块。如果你要重新抛出,那么只使用throw而不是throw ex,你会失去一些异常上下文,你通过throw ex抛出一个新的异常。
That code is exception handling, but pretty pointless handling. If all you are doing is re-throwing the exception then don't bother with the try\catch block. If you are going to rethrow then use just "throw" and not "throw ex" and you lose some exception context with you throw a new exception via "throw ex".


它看起来像原始开发人员想要处理异常但推迟了任务(因此重新抛出)。在任何情况下,结果代码都是丑陋的。
It looks the original developer wanted to handle the exceptions but postponed the task (hence the re-throw). In any case the resulting code is just ugly.


这只是处理异常的基本语法,还有另外一种方法来处理异常,但这主要用于..



protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

试试

{

//你的代码将被执行

}

catch(exception ex)

{

//记录异常



}

终于

{

//处理流程/任务



}

}

}
This is just the basic syntax to handle the Exception and there are another ways to handle the exception but this is mostly used..

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
// Your code which is to be execute
}
catch (Exception ex)
{
// Log the exception

}
finally
{
// for dispose the Process/ Task

}
}
}


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

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