C#抛出异常会引发另一个异常

编程入门 行业动态 更新时间:2024-10-22 13:50:16
本文介绍了C#抛出异常会引发另一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个异常抛出问题,我的程序通过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的记录,则此函数可以正常工作并返回我想要的内容。但如果表中没有包含记录,则会抛出另一个异常,如下图所示。 s7.postimg/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. s7.postimg/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#抛出异常会引发另一个异常

本文发布于:2023-11-11 23:31:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   抛出

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!