木子屋 Dnawo's BLOG

VS2008对引发异常和再次引发捕获异常的建议

👤 dnawo 📅 2009-04-22 👁 8137 👍 0 💬 0 🔄 本站原创
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace ClassLibrary1
{
    public class CommandLine
    {
        public static string Call(string name, params string[] args)
        {
            string returnValue = "";

            if (args.Length == 0)
                throw new Exception("缺少命令行参数!");

            using (Process commandline = new Process())
            {
                try
                {
                    commandline.StartInfo.UseShellExecute = false;
                    commandline.StartInfo.CreateNoWindow = true;
                    commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    commandline.StartInfo.RedirectStandardOutput = true;
                    commandline.StartInfo.FileName = name;
                    //commandline.StartInfo.WorkingDirectory = "";
                    commandline.StartInfo.Arguments = "";
                    //添加命令行参数
                    for (int i = 0; i < args.Length; i++)
                    {
                        commandline.StartInfo.Arguments += string.Format("\"{0}\" ", args[i]);//加引号防止参数带空格时出错
                    }

                    commandline.Start();
                    returnValue = commandline.StandardOutput.ReadToEnd().ToLower();
                    commandline.WaitForExit();
                    commandline.Close();
                }
                catch (Exception ex)
                {
                    commandline.Dispose();
                    throw ex;
                }
            }

            return returnValue;
        }
    }
}

运行代码分析后收到如下警告:

警告 4 CA2201 : Microsoft.Usage : 'CommandLine.Call(string, params string[])' 创建类型为 'Exception' 的异常,该异常类型不够具体,不应由用户代码引发。如有可能引发此异常实例,请使用其他异常类型。 E:\www\Demo\ClassLibrary1\CommandLine.cs 29 ClassLibrary1
警告 5 CA2200 : Microsoft.Usage : 'CommandLine.Call(string, params string[])' 再次引发捕获的异常并将其显式地指定为一个参数。请改用不带参数的“throw”以保留该异常最初引发时所在的堆栈位置。 E:\www\Demo\ClassLibrary1\CommandLine.cs 53 ClassLibrary1

修改如下(红色部分):

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace ClassLibrary1
{
    public class CommandLine
    {
        public static string Call(string name, params string[] args)
        {
            string returnValue = "";

            if (args.Length == 0)
                [color=Red]throw new ArgumentException("缺少命令行参数!");[/color]

            using (Process commandline = new Process())
            {
                try
                {
                    commandline.StartInfo.UseShellExecute = false;
                    commandline.StartInfo.CreateNoWindow = true;
                    commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    commandline.StartInfo.RedirectStandardOutput = true;
                    commandline.StartInfo.FileName = name;
                    //commandline.StartInfo.WorkingDirectory = "";
                    commandline.StartInfo.Arguments = "";
                    //添加命令行参数
                    for (int i = 0; i < args.Length; i++)
                    {
                        commandline.StartInfo.Arguments += string.Format("\"{0}\" ", args[i]);//加引号防止参数带空格时出错
                    }

                    commandline.Start();
                    returnValue = commandline.StandardOutput.ReadToEnd().ToLower();
                    commandline.WaitForExit();
                    commandline.Close();
                }
                [color=Red]catch
                {
                    commandline.Dispose();
                    throw;
                }[/color]
            }

            return returnValue;
        }
    }
}

建议:有时间多运行下代码分析工具,VS会给你一些很有用的建议,再针对这些建议对代码进行修改,这样能提高代码的整体性能。

评论(0)

暂无评论。

计算题
评论需审核通过后显示
← 上一篇 C#读取WinRAR安装路径 下一篇 → 新云允许链接本站的域名设置失效分析