异常处理quandry

时间:2018-02-12 16:28:03

标签: c#

当找不到数据库行时,我抛出一个新的异常。

被称为的类:

public ProfileBO retrieveProfileByCode(string profileCode)
{
    return retrieveSingleProfile("profile_code", profileCode);
}

private ProfileBO retrieveSingleProfile(string termField, string termValue)
{
    ProfileBO profile = new ProfileBO();
    //Query string is temporary.  Will make this a stored procedure.
    string queryString = " SELECT * FROM GamePresenterDB.gp.Profile WHERE " + termField + " = '" + termValue + "'";

    using (SqlConnection connection = new SqlConnection(App.getConnectionString()))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();

        if (reader.Read())
        {
            profile = castDataReadertoProfileBO(reader, profile);
        }
        else
        {
            // No record was selected.  log it and throw the exception (We'll log it later, for now just write to console.)
            Console.WriteLine("No record was selected from the database for method retrieveSingleProfile()");
            throw new InvalidOperationException("An exception occured.  No data was found while trying to retrienve a single profile.");
        }

        reader.Close();
    }
    return profile;
}

但是,当我在通话类中发现异常时,' e'现在为空。我究竟做错了什么?我相信这在Java中运行良好,所以C#必须以不同的方式处理它。

致电课程:

private void loadActiveProfile()
{
    try
    {
        ProfileBO profile = profileDAO.retrieveProfileByCode(p.activeProfileCode);
        txtActiveProfileName.Text = profile.profile_name;
    }
    catch (InvalidOperationException e)
    {

    }
}

3 个答案:

答案 0 :(得分:2)

现在所有代码都已放入问题中,您可以在“loadActiveProfile”方法之外移动try catch并将其放入“retrieveSingleProfile”。

private void loadActiveProfile()
{

        ProfileBO profile = profileDAO.retrieveProfileByCode(p.activeProfileCode);
        txtActiveProfileName.Text = profile.profile_name;
    }

删除了try catch ^

private ProfileBO retrieveSingleProfile(string termField, string termValue)
    {
      try {


        ProfileBO profile = new ProfileBO();
       //Query string is temporary.  Will make this a stored procedure.
        string queryString = " SELECT * FROM GamePresenterDB.gp.Profile WHERE " + termField + " = '" + termValue + "'";

        using (SqlConnection connection = new SqlConnection(App.getConnectionString()))
        {
            connection.Open();

            SqlCommand command = new SqlCommand(queryString, connection);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())                
            {
                profile = castDataReadertoProfileBO(reader, profile);
            } 
            else
            {
                // No record was selected.  log it and throw the exception (We'll log it later, for now just write to console.)
                Console.WriteLine("No record was selected from the database for method retrieveSingleProfile()");
                throw new InvalidOperationException("An exception occured.  No data was found while trying to retrienve a single profile.");
            }

            reader.Close();
        }
        return profile;
       }
      catch(InvalidOperationException e)
      { 

      }
    }

在正确的位置添加了try catch。

答案 1 :(得分:2)

您需要步骤进入 catch e块,将InvalidOperationException设置为抛出catch (System.InvalidOperationException e) { int breakPoint = 0; //<- set a breakpoint here. //Either you reach the breakpoint and have an InvalidOperationException, or you don't reach the breakpoint. MessageBox.Show(e.Message); }

InvalidOperationException

同时确保您抛出的System.InvalidOperationException实际上是host,而不是您的某些自定义类型&#34; InvalidOperationException&#34;。

答案 2 :(得分:0)

就像@Clemens所说,你需要显示所有相关的代码。

作为一个快速测试,这很好用:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Throwing error");
            ThrowException();
        }
        catch (InvalidOperationException e)
        {
            Console.WriteLine(e.Message);
        }

        Console.ReadKey(true);
    }

    static void ThrowException()
    {
        throw new InvalidOperationException("Blah blah blah");
    }
}