【问题标题】:how to tell app to stop processing after exception is thrown抛出异常后如何告诉应用程序停止处理
【发布时间】:2014-06-05 14:01:28
【问题描述】:

我的应用程序设置为连接到 MySQL 数据库。在线时效果很好。 我的查询是离线时,Visual Studio 会在消息框中显示“无法连接到数据库”异常消息后引发异常。

我把连接参数放在自己的类中。大多数声明都是在公共类声明下进行的。

// Command to open connection to database
    public bool OpenCon()
    {

        try
        {
            con.Open();
            return true;
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message);
            return false;
            //I was hoping to get the application to stop processing further after it failed to connect here.
        }

    }

    // Command to close connection to database
    public bool CloseCon()
    {
        try
        {
            con.Close();
            return true;
        }

        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message);
            return false;

        }

    }

    // This method is triggered by the 'signup_Click' event in the signup window
    public void Join(signup Signup)
    {
        // We now have the existing login window as "Signup".

        //Query to excecute
        query = "insert into temp.signup (member, first, second, third, surname, sex, ......) values ('" + Signup.member.Text + "', '" + Signup.first.Text + "','" + Signup.second.Text + "','" + Signup.third.Text + "','" + Signup.surname.Text + "', '" + Signup.sex.Text + "',....... );";
        //Declarations
        MySqlDataReader rdr;
        cmd = new MySqlCommand(query, con);



        // Excecution
        try
        {
            //Open the connection
            OpenCon();

            //Excecute the command
            //This is where the exception is thrown when the computer is offline, by the data reader.
            rdr = cmd.ExecuteReader();
            //Show the confirmation message
            Xceed.Wpf.Toolkit.MessageBox.Show("Saved");

            //CLose the connection
            CloseCon();
        }
        //If this fails, catch the exception
        catch (MySqlException ex)
        {
            //And show it in a messagebox
            MessageBox.Show(ex.Message);
            CloseCon();
        }
    }

我显然错了,因为这种情况正在发生,但我的印象是在您使用 return 关键字退出方法后处理停止,或者默认情况下在抛出异常时停止。

所以我的问题是,如果“OpenCon”方法返回 false(无法连接到数据库),我如何告诉它停止继续前进?

我希望它不要引发第二个异常,该异常会暂停我的应用程序,而只是等待进一步的指令。

【问题讨论】:

    标签: c# mysql


    【解决方案1】:

    你可以这样做

    if(OpenCon())
    {
                //Excecute the command
                //This is where the exception is thrown when the computer is offline, by the data reader.
                rdr = cmd.ExecuteReader();
                //Show the confirmation message
                Xceed.Wpf.Toolkit.MessageBox.Show("Saved");
    
                //CLose the connection
                CloseCon();
    }
    

    很高兴你有一个布尔返回值,检查它并采取进一步的行动。

    【讨论】:

      【解决方案2】:

      如果抛出异常,应用程序不会终止。用外行的话来说,如果在应用程序的任何地方都没有捕获到异常,则 CLR 会强制终止应用程序。由于您已经处理了异常并且没有重新抛出它,因此 CLR 假定您已经“处理”了异常并继续执行其余代码。

      我假设您想在 catch 块中强制终止应用程序。这是非常糟糕的做法,超出了异常处理的全部目的。您应该优雅地通知用户并继续执行替代方案。 但是为了您的知识,您可以使用Environment.FailFast 立即强制退出应用程序。强烈建议不要这样做,因为它现在终止应用程序,甚至不执行任何 finally 块,除非是灾难性故障,否则应避免这样做。

      最好抛出一个自定义异常,让它冒泡,这样你就可以在 UI 层安全地捕获它并通知用户。

      【讨论】:

        猜你喜欢
        • 2010-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        • 2011-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多