10: Exception Handling in C#.NET

Creation QR Code 2d barcode in C#.NET 10: Exception Handling
10: Exception Handling
Create QR Code In C#
Using Barcode maker for Visual Studio .NET Control to generate, create QR-Code image in .NET framework applications.
Defining Custom Exceptions
Draw Barcode In C#
Using Barcode generation for Visual Studio .NET Control to generate, create bar code image in VS .NET applications.
Once throwing an exception becomes the best course of action, it is preferable to use framework exceptions because they are well established and understood Instead of throwing a custom invalid argument exception, for example, it is preferable to use the SystemArgumentException type However, if the developers using a particular API will take special action the exception-handling logic will vary to handle a custom exception type, for instance it is appropriate to define a custom exception For example, if a mapping API receives an address for which the ZIP Code is invalid, instead of throwing SystemArgumentException, it may be better to throw a custom InvalidAddressException The key is whether the caller is likely to write a specific InvalidAddressException catch block with special handling rather than just a generic SystemArgumentException catch block Defining a custom exception simply involves deriving from System Exception or some other exception type Listing 104 provides an example
QR Code Maker In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
Listing 104: Creating a Custom Exception
Print QR Code In .NET
Using Barcode generator for Visual Studio .NET Control to generate, create QR Code ISO/IEC18004 image in .NET framework applications.
class DatabaseException : SystemException { public DatabaseException( SystemDataSqlClientSQLException exception) { InnerException = exception; // } public DatabaseException( SystemDataOracleClientOracleException exception) { InnerException = exception; // } public DatabaseException() { // } public DatabaseException(string message) { // }
QR Code JIS X 0510 Maker In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create QR Code image in .NET applications.
Defining Custom Exceptions
Make Barcode In C#
Using Barcode creator for .NET Control to generate, create barcode image in .NET applications.
public DatabaseException( string message, Exception innerException) { InnerException = innerException; // } }
Making Bar Code In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create bar code image in VS .NET applications.
This custom exception might be created to wrap proprietary database exceptions Since Oracle and SQL Server (for example) each throw different exceptions for similar errors, an application could define a custom exception that standardizes the database-specific exceptions into a common exception wrapper that the application can handle in a standard manner That way, whether the application was using an Oracle or a SQL Server backend database, the same catch block could be used to handle the error higher up the stack The only requirement for a custom exception is that it derives from SystemException or one of its descendents However, there are several more good practices for custom exceptions
Code39 Maker In C#.NET
Using Barcode generation for VS .NET Control to generate, create Code39 image in Visual Studio .NET applications.
All exceptions should use the Exception suffix This way, their
Code 128 Code Set A Maker In C#
Using Barcode creation for VS .NET Control to generate, create Code 128 Code Set C image in VS .NET applications.
purpose is easily established from the name
Printing DataMatrix In Visual C#
Using Barcode drawer for VS .NET Control to generate, create ECC200 image in VS .NET applications.
Generally, all exceptions should include constructors that take no
Paint Barcode In VB.NET
Using Barcode generator for .NET Control to generate, create bar code image in .NET framework applications.
parameters, a string parameter, and a parameter set of a string and an inner exception Furthermore, since exceptions are usually constructed within the same statement in which they are thrown, any additional exception data should also be allowed as part of the constructor (The obvious exception to creating all these constructors is if certain data is required and a constructor circumvents the requirements)
Encoding ECC200 In VS .NET
Using Barcode printer for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
The inheritance chain should be kept relatively shallow (with fewer
Creating Barcode In VB.NET
Using Barcode generator for VS .NET Control to generate, create bar code image in .NET framework applications.
than approximately five levels) The inner exception serves an important purpose when rethrowing an exception that is different from the one that was caught For example, if a SystemDataSqlClientSqlException is thrown by a database call but is caught within the data access layer to be rethrown as a DatabaseException, then the DatabaseException constructor that takes the SqlException
European Article Number 13 Maker In Java
Using Barcode drawer for Java Control to generate, create EAN / UCC - 13 image in Java applications.
10: Exception Handling
Bar Code Creation In Java
Using Barcode printer for Java Control to generate, create bar code image in Java applications.
(or inner exception) will save the original SqlException in the InnerException property That way, when requiring additional details about the original exception, developers can retrieve the exception from the InnerException property (for example, exceptionInnerException)
Painting USS Code 128 In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
ADVANCED TOPIC Serializable Exceptions Serializable objects are objects that the runtime can persist into a stream a file stream, for example and then reinstantiate out of the stream In the case of exceptions, this may be necessary for certain distributed communication technologies To support serialization, exception declarations should include the SystemSerializableAttribute attribute or they should implement ISerializable Furthermore, they must include a constructor that takes SystemRuntimeSerializationSerializationInfo and SystemRuntimeSerializationStreamingContext Listing 105 shows an example of using SystemSerializableAttribute
Drawing ECC200 In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create Data Matrix ECC200 image in .NET framework applications.
Listing 105: Defining a Serializable Exception
Decode EAN-13 In .NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
// Supporting serialization via an attribute [Serializable] class DatabaseException : SystemApplicationException { // // Used for deserialization of exceptions public DatabaseException( SerializationInfo serializationInfo, StreamingContext context) { // } }
Bar Code Printer In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create bar code image in ASP.NET applications.
The preceding DatabaseException example demonstrates both the attribute and the constructor requirement for making an exception serializable
Create Code 3/9 In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create Code 3 of 9 image in ASP.NET applications.
Defining Custom Exceptions
BEGINNER TOPIC Checked and Unchecked Conversions As we first discussed in a 2 Advanced Topic, C# provides special keywords for marking a code block with instructions to the runtime of what should happen if the target data type is too small to contain the assigned data By default, if the target data type cannot contain the assigned data, then the data will truncate during assignment For an example, see Listing 106