[英]C# try catch confusion
I'm very new to C#. 我对C#很新。
When I try catch something like this: 当我尝试捕捉这样的东西时:
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch(SqlException ex)
{
MessageBox.Show("there was an issue!");
}
How do I know if the problem happened with the Open
or ExecuteNonQuery
? 我如何知道
Open
或ExecuteNonQuery
是否出现问题?
What if I called a bunch of other non-SQL
stuff in the Try
? 如果我在
Try
调用了一堆其他non-SQL
内容怎么办?
How would I know which failed? 我怎么知道哪个失败了?
What does SqlException
mean over regular Exception
? SqlException
对常规Exception
什么?
How would SqlException
handle non-SQL related errors if I had such code in the Try
block? 如果我在
Try
块中有这样的代码, SqlException
将如何处理非SQL相关的错误?
You can add multiple catches after the try block 您可以在try块后添加多个catch
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch(SqlException ex)
{
MessageBox.Show("there was an issue!");
}
catch(Exception ex)
{
MessageBox.Show("there was another issue!");
}
The important rule here to to catch the most specific exception higher up and the more general ones lower down 这里有一个重要的规则来捕捉更高的特定异常和更低级的更普遍的异常
What if I called a bunch of other non-SQL stuff in the TRY? 如果我在TRY中调用了一堆其他非SQL内容怎么办? How would I know which failed?
我怎么知道哪个失败了?
This will be based on the exception that is thrown by the operation. 这将基于操作引发的异常。 Like I say, catch the most specific exceptions first and get more general as you go down.
就像我说的那样,首先捕捉最具体的例外情况,然后在下降时更加通用。 I suggest going to look at the MSDN documentation for methods that you think may thrown an exception.
我建议您查看MSDN文档,了解您认为可能引发异常的方法。 This documentation details what exceptions are thrown by methods in which circumstances.
本文档详细说明了在哪些情况下方法引发的异常。
What does SQLEXCEPTION mean over regular EXCEPTION? SQLEXCEPTION对常规EXCEPTION的意义是什么?
SQLException
is an extension of the normal Exception
class that is only thrown in certain circumstances. SQLException
是普通Exception
类的扩展,仅在某些情况下抛出。
How would SQLEXCEPTION handle non-SQL related errors if I had such code in the TRY block? 如果我在TRY块中有这样的代码,SQLEXCEPTION如何处理非SQL相关的错误?
So to answer your question, the blocks that you had set up would not catch any exception that was not a SQLException
or extended SQLException
. 因此,要回答您的问题,您设置的块将不会捕获任何不是
SQLException
或扩展SQLException
异常。
The quick answer is that you can have many catch() portions that catch specific exceptions. 快速回答是,您可以拥有许多catch()部分来捕获特定的异常。
try{
}
catch (SqlException sqlEx)
{
//deal with sql error
}
catch (NullArgumentException)
{
//Deal with null argument
}//etc
finally
{
//do cleanup
}
what you really want to be doing is putting things in the try that focus around a specific exception that could happen. 你真正想要做的是把事情放在试图中,关注可能发生的特定异常。 You also want to rather have try-catch blocks around boundary code (where you do not have control over what happens) and handle your own errors elegantly.
您还希望在边界代码周围使用try-catch块(您无法控制发生的情况)并优雅地处理您自己的错误。
You can see in which part of code your Exception occured by catching the Exception of SQL 通过捕获SQL的异常,您可以看到您的Exception出现在代码的哪个部分
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch(SqlException ex) // This will catch all SQL exceptions
{
MessageBox.Show("Execute exception issue: "+ex.Message);
}
catch(InvalidOperationException ex) // This will catch SqlConnection Exception
{
MessageBox.Show("Connection Exception issue: "+ex.Message);
}
catch(Exception ex) // This will catch every Exception
{
MessageBox.Show("Exception Message: "+ex.Message); //Will catch all Exception and write the message of the Exception but I do not recommend this to use.
}
finally // don't forget to close your connection when exception occurs.
{
connection.Close();
}
InvalidOperationException: InvalidOperationException异常:
Acording to MSDN: Cannot open a connection without specifying a data source or server. 根据MSDN:无法在未指定数据源或服务器的情况下打开连接。 or The connection is already open.
或连接已打开。
I do not recommend you to write whole Exception message
to the UI as it is more vulnerable for hacking the SQL Database 我不建议您将完整的
Exception message
写入UI,因为它更容易被黑客攻击SQL数据库
SqlException
according to MSDN : 根据MSDN的
SqlException
:
This class is created whenever the .NET Framework Data Provider for SQL Server encounters an error generated from the server. 只要SQL Server的.NET Framework数据提供程序遇到从服务器生成的错误,就会创建此类。 (Client side errors are thrown as standard common language runtime exceptions.) SqlException always contains at least one instance of SqlError.
(客户端错误作为标准公共语言运行时异常抛出。)SqlException始终包含至少一个SqlError实例。
You can use several catch block for a single try block to handle different possible exceptions. 您可以对单个try块使用多个catch块来处理不同的可能异常。
If you use exception and print message for that exception then your application will not break but will show message for the occurred exception. 如果对该异常使用异常和打印消息,则应用程序不会中断,但会显示已发生异常的消息。 Actually each catch block will define different exception, and you can write a specific exception's catch block only when you are sure that, the particular type of exception may happen.
实际上每个catch块都会定义不同的异常,只有在您确定可能发生特定类型的异常时,才能编写特定异常的catch块。 otherwise declare a single catch block like
catch(Exception ex)
which will be caught for any kind of exception and track error using ex.toString()
message. 否则声明一个像
catch(Exception ex)
这样的catch块,它会被任何类型的异常捕获并使用ex.toString()
消息跟踪错误。 You can also use a breakpoint in the try block to get particular line which have caused the exception. 您还可以在try块中使用断点来获取导致异常的特定行。
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (NullArgumentException ex)
{
//Deal with null argument
ex.toString();
}
catch (NumberFormatException ex)
{
//Deal with null argument
ex.toString();
}
catch(SqlException ex)
{
MessageBox.Show("there was an issue!");
}
catch(Exception ex)
{
MessageBox.Show(""+ex.toString());
}
Thanks 谢谢
To further determine the issue, you can have this syntax: 要进一步确定问题,您可以使用以下语法:
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show("Error:" + ex.Message); // This will display all the error in your statement.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.