Custom Exceptions in C #

Source: Internet
Author: User
Tags binary to decimal modifier string format
When you write a method, do you encounter errors in data entry? If the string format cannot be converted to an integral type. However, the exception that handles these errors. NET has already helped us to write well. But if a string is entered you want to determine if he is a binary number. What do you do with this anomaly, and return an error ID? If you write a function that is binary to decimal, the value returned is an integer. This error returns a value if it identifies ... haha,,, you may say several or dozens of ways to solve ...
Here's a way to solve my problem by exception ... If you want to see how I handle it, or want to know how to customize an exception hint for yourself. Then go on down ...
creating a custom exception can be derived primarily from two classes:1.ApplicationException class
2.system.exception class above two kinds of specific how to use, but also to be inspected oh ... Today I use the class of System.Exception to derive my own class oh.
Code:
1. Create a custom exception class
public class UserDefinedException:System.Exception//See System.Exception Class not inherited
{
private string message;//error message//Below is the constructor ... You can write a few more yourself. It might be more flexible to turn him into polymorphism.
Public userdefinedexception (String message): Base ()//Here is the rewrite constructor
{
message = message;
//
TODO: Add constructor logic here
//
}
<summary>
Overriding exception error messages
</summary>
<returns></returns>
public override string ToString ()//Overriding the ToString method, see the override description below
{
return message;
}
} Override DescriptionOh,:(MSDN.
Use the override modifier to modify a method, property, indexer, or event. An overriding method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is called the overriding base method. The overridden base method must have the same signature as the overriding method. Non-virtual or static methods cannot be overridden. The overriding base method must be virtual, abstract, or overridden. An overriding declaration cannot change the accessibility of a virtual method. Overriding methods and virtual methods must have the same access-level modifier. Override methods cannot be modified using the following modifiers: The new static virtual abstract Override property declaration must specify an access modifier, type, and name that is identical to the inherited property, and the overriding property must be virtual, abstract, or overridden. 2. Use of custom exceptions:
public static int Binarysystemtoalgorism (string binarysystemvalue)
{
Binarysystemvalue = Binarysystemvalue.trim ();
int algorism = 0;
int len = binarysystemvalue.length;
int temp;
int temppow = 0; int col = 0;
for (int index = len-1;index > -1;index-)
{
temp = Convert.ToInt32 (Binarysystemvalue[col]. ToString ());
if (!) ( Temp = 0 | | temp = 1))
{
See not here Oh, because I write in a different project so take the namespace on it.
throw new Farseekclass.userdefinedexception ("Error, binary input error!");
}
Col + 1;
Temppow = Pow (2,index);
Algorism + + temp * TEMPPOW;

}
return algorism;
} 3. Receive exception messages outside:
Try
{
This. TextBox1.Text = FarSeekClass.DataSwitch.BinarySystemToAlgorism (TH. Text). ToString ();
}
catch (Farseekclass.userdefinedexception ex)/note this way, the exception class behind the catch is the class name we defined ourselves
{
Response.Write (ex. ToString ());
The above is my most valuable tonight.
Hey.... Give me more advice oh. Remember to leave a message, passing also leave a shadow Oh.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.