Retention Order

When using the framework method, the description that you get when you select above it will list the exception (s), possibly raised.

Does it matter if you write catch handlers in this order? For example. SqlConnection.Open () can throw:

System.InvalidOperationException System.Data.SqlClient.SqlException

It is listed in that order. Should my catchers keep this order? If so, why?

In addition, some methods such as SqlDataAdapter.Fill () return an int, but no error is reported to Visual Studio unless I have a variable to store the return value. Why?

thanks

+3
source share
4 answers

, , .

, SQL Server SQL, , , SqlException, , .

. ( ), , . .

+3

, , , , , ( ).

catch , , , , , , catch, ArgumentException:

try
{
  throw new ArgumentException("foo");
}
catch (Exception ex)
{
}
catch (ArgumentException aex)
{
  // Will never be executed.
}
+8

( Mitch, , .)

, .

, System.Exceptions System.InvalidOperationExceptions, InvalidOperationException, System.Exception. catch System.Exception, , catch InvalidOperationException .

+5

catch , , , .

catch (Exception ex) {
    strMessage = ex.Message.ToString();
} catch (ArgumentNullException aex) {
    strMessage = aex.Message;
}

The compiler will report an error in the above case, and the error will be like this: the previous catch clause already catches all exceptions of this or super-type System.Exception.

+1
source

Source: https://habr.com/ru/post/1712963/


All Articles