[ Pobierz caÅ‚ość w formacie PDF ] .Security.VerificationException Security policy requires code to be type safe, and thecode could not be verified as type safe.UnauthorizedAccessException The operating system denied access because of an input/output or security error.In addition to these basic exceptions, the.NET Framework defines several other exception classesthat have more specialized use.For example, SQL exceptions can occur when a program works withSQL Server databases.The following sections describe some of the more common and useful of these specialized excep-tion classes.SQL ExceptionsSQL Server uses the single class System.Data.SqlClient.SqlException to represent all errors andexceptions.You can use the SqlException object s properties to determine what has gone wrongand how severe it is.Table 6-3 describes some of the most useful SqlException class properties.Exception Handling 243X'TABLE 6-3: Useful SqlException PropertiesPROPERTY DESCRIPTIONClass A number between 0 and 25 giving the type of error.Values 20 through 25 arefatal and the database connection closes.The values indicate0 10: Information messages rather than errors.11 16: User problems that can be fixed by the user.17: SQL Server has run out of a configurable resource such as locks.The DBA mayfix this.18: A nonfatal internal software problem.19: SQL Server has exceeded a nonconfigurable resource limit.20: A problem occurred in a statement issued by the current process.21: SQL Server encountered a problem that affects all processes in a database.22: A table or index has been damaged.23: The database is suspect.24: Hardware problem.25: System error.LineNumber Returns the line number within the T-SQL command batch or stored procedurethat caused the error.Message Returns a message describing the problem.Number Returns the error number.Procedure Returns the name of the stored procedure or remote procedure call that causedthe error.The System.Data.Common.DbException class is the parent class of SqlException and three otherclasses that return similar information for other database types.The following list summarizes thethree other child DbException child classes:¤'¤'System.Data.Odbc.OdbcException: Errors in ODBC databases¤'¤'System.Data.OleDb.OleDbException: Errors in OLE DB databases¤'¤'System.Data.OracleClient.OracleException: Errors in Oracle databasesAll these classes provide a Message property that gives information about the exception, althoughthey do not provide the Class, LineNumber, Number, and Procedure properties provided by theSqlException class.244 CHAPTER 6 WORKING WITH DELEGATES, EVENTS, AND EXCEPTIONSX'Overflow ExceptionsBy default, a C# program does not throw an exception if an arithmetic operation causes an integeroverflow.If the operands are integral or decimal, the program discards any extra bits, returns atruncated result, and continues running as if nothing had gone wrong.In that case you might not beaware that the result is gibberish.You can make the program throw an OverflowException either by using a checked block or byusing the Advanced Build Settings dialog.For more information, see the section Using Wideningand Narrowing Conversions in Chapter 4, Using Types.A C# program also doesn t throw an exception if a floating point operation causes an overflow orunderflow, or if it produces the special value NaN (which stands for not a number ).The floating point types define static properties PositiveInfinity, NegativeInfinity, and NaN.You can compare a floating point variable to the PositiveInfinity and NegativeInfinity values.However, if you compare a variable to NaN, the result is always false.(Even float.NaN == float.NaN returns false.)Instead of trying to compare results to the special values, it is better to use the type s methods todetermine whether a variable holds one of these special values.Table 6-4 describes these methods.TABLE 6-4: Floating Point Special Value MethodsMETHOD DESCRIPTIONIsInfinity Returns true if the value is PositiveInfinity or NegativeInfinityIsNaN Returns true if the value is NaNIsNegativeInfinity Returns true if the value is NegativeInfinityIsPositiveInfinity Returns true if the value is PositiveInfinityUsing the special value methods listed in Table 6-4 makes code easier to understand and protects thecode in case the special values such as PositiveInfinity change in some later version of.NET, forexample if the float data type moves to 64 bits.REAL-WORLD CASE SCENARIO FactorialsThe factorial of a number N is written N! and is defined by N! = 1 × 2 × 3 ×.× N.Write a Factorialmethod that uses long integers to calculate a number s factorial.Watch for the following errors:¤'¤'N isn t an integer¤'¤'N = 0.if (n x * x;b.result = (x) => return x * x;c.result = x => x * x;d.Both a and c are correct.10.Which of the following statements about statement lambdas is false?a.A statement lambda can include more than one statement.b
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|