30Dec/100
Throwing and Catching the Custom Exception via C#
Once the custom exception is created, it may be thrown and caught in the same way as any other exception. This includes catching the exact exception or the more generic types that it is derived from. Indeed, one custom exception may derive from another to create a full hierarchy of application exceptions for flexible handling.
The final code sample combines the custom exception with the code for a simple console application. This program throws the custom exception within a try block. The exception message is outputted within the following catch block.
using System;
namespace BlackWasp
{
class TestApp
{
static void Main(string[] args)
{
try
{
// Throw a test exception
throw new InvalidPrinterMarginsException
("The margins are too small");
}
catch (InvalidPrinterMarginsException ex)
{
Console.WriteLine(ex.Message);
}
}
}
// Custom exception class
class InvalidPrinterMarginsException : ApplicationException
{
// Use the default ApplicationException constructors
public InvalidPrinterMarginsException() : base() {}
public InvalidPrinterMarginsException(string s) : base(s) {}
public InvalidPrinterMarginsException(string s, Exception ex)
: base(s, ex) {}
}
}
Recent Posts
- FIX: Error message when you run a “BULK INSERT” statement on a table that does not have a clustered index in SQL Server 2005 or SQL Server 2008: “spid1005 Error: 5243, Severity: 22, State: 1 An i …
- Custom software development- makes work easy and simple
- 4 Things to Consider For Outdoor Advertising
- Barter In A Slow Economy
- Error message when you try to install SQL Server 2008 if the path of the SQL Server 2008 installation media contains a number sign (#) character: “Could not load file or assembly ‘<Path>\Mi …
- Employee Performance Appraisal System: An Overview
- CMYK versus RGB color spectrum
- Description of the Jet 4.0 Database Engine hotfix package for Windows XP SP2, Windows Server 2003 SP1, Windows Server 2003 SP2, Windows Vista, Windows Vista SP1, and Windows Server 2008: October …
- Adobe training in Sydney from Creative Mentor
- Business Card Do's and Don'ts
Tag Cloud
-
application
ASP
assembly
career
class
code
company
constructor
control
Database
difference
Don
employer
error
Exception
hiring manager
information
interview
interviewer
job
job seekers
manager
method
NET
page
person
position
Process
property
question
quot
response
Serialization
Server
session state
site
SQL
State
system
time
type
void
way
web
work
BlogRoll
Archives
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009