104: Creating a Custom Exception in C#

Making PDF 417 in C# 104: Creating a Custom Exception
Listing 104: Creating a Custom Exception
Paint PDF 417 In Visual C#.NET
Using Barcode drawer for .NET framework Control to generate, create PDF417 image in .NET framework applications.
class DatabaseException : SystemException { public DatabaseException( SystemDataSqlClientSQLException exception)
Generating Bar Code In Visual C#.NET
Using Barcode creation for .NET Control to generate, create barcode image in .NET applications.
10: Exception Handling
PDF-417 2d Barcode Creator In .NET
Using Barcode encoder for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
{ InnerException = exception; // } public DatabaseException( SystemDataOracleClientOracleException exception) { InnerException = exception; // } public DatabaseException() { // } public DatabaseException(string message) { // } public DatabaseException( string message, Exception innerException) { InnerException = innerException; // } }
Encoding PDF 417 In .NET Framework
Using Barcode creation for VS .NET Control to generate, create PDF417 image in .NET framework 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
PDF 417 Maker In Visual Basic .NET
Using Barcode creator for .NET framework Control to generate, create PDF-417 2d barcode image in .NET applications.
All exceptions should use the Exception suffix This way, their pur-
Bar Code Encoder In Visual C#
Using Barcode maker for VS .NET Control to generate, create barcode image in .NET framework applications.
pose is easily established from the name
Bar Code Generation In C#.NET
Using Barcode printer for .NET Control to generate, create bar code image in VS .NET applications.
Defining Custom Exceptions Generally, all exceptions should include constructors that take no
Code 3/9 Encoder In C#.NET
Using Barcode drawer for .NET framework Control to generate, create USS Code 39 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)
Code128 Printer In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create Code 128 Code Set B image in Visual Studio .NET applications.
The inheritance chain should be kept relatively shallow (with fewer
DataMatrix Creator In C#.NET
Using Barcode generation for VS .NET Control to generate, create DataMatrix image in VS .NET 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 (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 (eg, exceptionInnerException)
Generating Universal Product Code Version A In VS .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create UPC Code image in .NET applications.
ADVANCED TOPIC Serializable Exceptions Serializable objects are objects that the runtime can persist into a stream a filestream, 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
EAN13 Creator In Java
Using Barcode drawer for Java Control to generate, create EAN13 image in Java applications.
Listing 105: Defining a Serializable Exception
Code 128 Code Set C Creator In VS .NET
Using Barcode maker for Visual Studio .NET Control to generate, create Code 128 Code Set C image in VS .NET applications.
// Supporting serialization via an attribute [Serializable]
Make GS1 - 13 In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create EAN 13 image in ASP.NET applications.
10: Exception Handling
Printing Data Matrix 2d Barcode In VB.NET
Using Barcode creation for VS .NET Control to generate, create DataMatrix image in .NET framework applications.
class DatabaseException : SystemApplicationException { // // Used for deserialization of exceptions public DatabaseException( SerializationInfo serializationInfo, StreamingContext context) { // } }
Drawing Barcode In VS .NET
Using Barcode generator for .NET framework Control to generate, create barcode image in VS .NET applications.
The preceding DatabaseException example demonstrates both the attribute and the constructor requirement for making an exception serializable
Barcode Drawer In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create bar code image in .NET framework applications.
BEGINNER TOPIC Checked and Unchecked Conversions As 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, the data will truncate during assignment For an example, see Listing 106
Generating Barcode In .NET
Using Barcode generation for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Listing 106: Overflowing an Integer Value
Making Barcode In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create bar code image in ASP.NET applications.
using System; public class Program { public static void Main() { // intMaxValue equals 2147483647 int n = intMaxValue; n = n + 1 ; SystemConsoleWriteLine(n); } }
GS1 - 12 Generation In Java
Using Barcode drawer for Java Control to generate, create UPC-A image in Java applications.
The results of Listing 106 appear in Output 101
Defining Custom Exceptions
OUTPUT 101:
The code in Listing 106 writes the value -2147483648 to the console However, placing the code within a checked block or using the checked option when running the compiler will cause the runtime to throw an exception of type SystemOverflowException The syntax for a checked block uses the checked keyword, as shown in Listing 107
Listing 107: A Checked Block Example
using System; public class Program { public static void Main() { checked { // intMaxValue equals 2147483647 int n = intMaxValue; n = n + 1 ; SystemConsoleWriteLine(n); } } }