C#抛出异常会引发另一个异常 [英] C# throwing exceptions throws another exception
问题描述
我有一个异常抛出问题,我的程序通过OleDB连接与访问数据库一起工作。
这是我的函数导致错误。
Im having a exception throwing problem with my program which works with a access database through OleDB connection.
Here is my function which causes the error.
public static string getNICByID(int ID)
{
//validation params
if (ID <= 0)
throw new Exception("Invalid ID");
//if (!isRegNumExists(ID))
// throw new Exception("No records");
const string query = @"select NICNumber from Student where StudentRegNo = @_regNum";
try
{
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
cmd.Parameters.Add("@_regNum", OleDbType.Integer).Value = ID;
object obj = cmd.ExecuteScalar();
if (obj != null)
return obj.ToString();
else
throw new Exception("No records");
}
}
}
catch { throw; }
如果数据表包含有关ID的记录,则此函数可以正常工作并返回我想要的内容。但如果表中没有包含记录,则会抛出另一个异常,如下图所示。
http://s7.postimg.org/w4j72ie8b/Untitled.png [ ^ ]
我的代码有什么问题?
提前谢谢。
This functions works fine and returns what I want if the data table contains a record with respect to the ID. But if the table doesnt contain a record it throws another exception like the picture below.
http://s7.postimg.org/w4j72ie8b/Untitled.png[^]
What's the wrong with my code ?
Thanks in advance.
推荐答案
这是因为当你在数据库中找不到记录时,你本人就会在你的代码中抛出这个异常。
That is because you, yourself, personally are throwing this exception in your code when there is no record found in the database.
if (obj != null)
return obj.ToString();
else
throw new Exception("No records");
这行代码旨在满足您的需求。它会看到,如果有任何记录,则以其他方式抛出异常;以参数为原因或消息
。
要忽略引发的异常,请删除此行,
This line of code is intended to do what you're seeing. It will see, if there is any record, ortherwise throw the exception; with the parameter as the reason or Message
.
To ignore the exception being raised, please remove this line,
throw new Exception("No records");
然后它不会抛出异常,而是你可以用这个值显示一条消息弹出给用户。
Then it won't throw the exception, instead you can show a message pop up to the user with this value.
你'再次在你的catch()部分再次抛出Excetion。
You're throwing the Excetion again in your catch() section.
try
{
// your code
throw New Exception ("no recors")
}
catch (throw)
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
其他两个答案显示了正在发生的事情。我会做的是
将你的试试块更改为
The other two answers are showing you what is going on. What I would do is
change your try catch block to
try
{
if (obj != null)
return obj.ToString();
else
throw new Exception("No records");
}
catch(Exception ex)
{
//put a break point on this line below
String message = ex.message;
}
我会在字符串消息行上设置断点,然后在调试时检查ex。消息,因为它会告诉你错误信息应该是没有记录
我个人也会将你的异常更改为更合适的东西,比如SQLExcpetion而不是catch所有异常
I would put a breakpoint on the string message line and then while debugging check the ex.message as it will show you that the error message should be "No Records"
I personally would also change your exception to something more appropriate such as SQLExcpetion rather than the catch all Exception
这篇关于C#抛出异常会引发另一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!