try-catch 块未捕获异常
try-catch block not catching the Exception
我正在使用 try-catch 块来识别数据库中是否缺少某些数据。在我开始使用 while 检查用户输入之前,它运行良好,但现在 try-catch 块不会被执行(while 在 try-catch 内部)。
我试图捕获的异常是 OleDbDataReader 在查询 returns nothing.
时抛出的 InvalidOperationException
try
{
string _cmd = "SELECT razao_social FROM tblImobiliarias WHERE cnpj ='" + ValorConsulta.Text + "'";
string conn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\10.7.41.153\Apoio\Davi_Database\Base_Imo.accdb";
OleDbConnection Connection = new OleDbConnection(conn);
OleDbCommand Command = new OleDbCommand(_cmd, Connection);
Connection.Open();
OleDbDataReader reader = Command.ExecuteReader();
while (reader.Read())
{
int _resMatriz = 0;
DialogResult result;
result = MessageBox.Show(
"Encontrado imobiliária: "
+ reader[_resMatriz]
+ ".\nEstá correto?", "Pesquisa", MessageBoxButtons.YesNo);
_resMatriz++;
if (result == System.Windows.Forms.DialogResult.Yes)
{
MessageBox.Show("CarregaImo()");
break;
}
else
{
continue;
}
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show(
"O CNPJ "
+ ValorConsulta.Text
+ " não foi localizado no sistema.\nOs dados não podem ter pontuação. Tente novamente",
"Pesquisa",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
throw new InvalidOperationException();
}
我建议不要将 Exception
用于正常的应用程序逻辑。如果您的 reader 没有结果,您可以像这样使用 'HasRows` 属性:
OleDbDataReader reader = Command.ExecuteReader();
if(reader.HasRows)
{
// Do what you need if there are rows
}
else // Instead of an exception handler
{
MessageBox.Show("O CNPJ " + ValorConsulta.Text + " não foi localizado no sistema.\nOs dados não podem ter pontuação. Tente novamente", "Pesquisa", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new InvalidOperationException(); // If you must throw the exception
}
我正在使用 try-catch 块来识别数据库中是否缺少某些数据。在我开始使用 while 检查用户输入之前,它运行良好,但现在 try-catch 块不会被执行(while 在 try-catch 内部)。 我试图捕获的异常是 OleDbDataReader 在查询 returns nothing.
时抛出的 InvalidOperationExceptiontry
{
string _cmd = "SELECT razao_social FROM tblImobiliarias WHERE cnpj ='" + ValorConsulta.Text + "'";
string conn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\10.7.41.153\Apoio\Davi_Database\Base_Imo.accdb";
OleDbConnection Connection = new OleDbConnection(conn);
OleDbCommand Command = new OleDbCommand(_cmd, Connection);
Connection.Open();
OleDbDataReader reader = Command.ExecuteReader();
while (reader.Read())
{
int _resMatriz = 0;
DialogResult result;
result = MessageBox.Show(
"Encontrado imobiliária: "
+ reader[_resMatriz]
+ ".\nEstá correto?", "Pesquisa", MessageBoxButtons.YesNo);
_resMatriz++;
if (result == System.Windows.Forms.DialogResult.Yes)
{
MessageBox.Show("CarregaImo()");
break;
}
else
{
continue;
}
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show(
"O CNPJ "
+ ValorConsulta.Text
+ " não foi localizado no sistema.\nOs dados não podem ter pontuação. Tente novamente",
"Pesquisa",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
throw new InvalidOperationException();
}
我建议不要将 Exception
用于正常的应用程序逻辑。如果您的 reader 没有结果,您可以像这样使用 'HasRows` 属性:
OleDbDataReader reader = Command.ExecuteReader();
if(reader.HasRows)
{
// Do what you need if there are rows
}
else // Instead of an exception handler
{
MessageBox.Show("O CNPJ " + ValorConsulta.Text + " não foi localizado no sistema.\nOs dados não podem ter pontuação. Tente novamente", "Pesquisa", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new InvalidOperationException(); // If you must throw the exception
}