C # Advanced Programming 82-day----user-defined exception class

Source: Internet
Author: User

User-defined Exception class

There have been a lot of questions about anomalies , and now it's time to give you a why did about what you're doing when you define an exception .

?

Why do you have to define your own exception class ?

Use your toes and make sure , is to define our own anomalies. , you define the exception class to inherit from applicationexception?

The first exception (Exception) is divided into two categories , One is the exception class defined in advance , and the latter is the class to inherit when the user defines the exception class. .

Case :

?

Using? System;

Using? System.Collections.Generic;

Using? System.Linq;

Using? System.Text;

Using? System.Threading.Tasks;

?

Namespace? define the exception class yourself

{

???? Class? Program

???? {

???????? Static?void? Main (String[]?args)

???????? {

???????????? or the case of the previous sentence?

???????????? Console.WriteLine (" Enter a number from 0 to 5 :?");

???????????? Try

???????????? {

???????????????? you must have known that.

???????????????? If you don't know , You can check out the meaning of Parse , and don't expect me to say anything. !

???????????????? Int?num?=?int. Parse (Console.ReadLine ());

???????????????? If? (num?<?0?| |? NUM?>?5)

???????????????? {

???????????????????? Throw?new? MyException (" Are you funny ? ? " tease me. ?

");

????????????????}

????????????}

???????????? Catch? (Myexception?ex)

???????????? {

???????????????? Console.WriteLine (ex. Message);

????????????}

???????????? Catch? (Exception?ex)

???????????? {

???????????????? Console.WriteLine (ex. Message);

????????????}

???????????? Finally

???????????? {

???????????????? Console.WriteLine (" I'm still the finally?!? of the Ox X ");????????????

????????????}

???????????? Console.readkey ();

????????}

????}

???? Public?class? MyException?:? ApplicationException?

{

Public? MyException? (){}

???????? Public? MyException (string?message)?:? base (Message)? {?} Do you know what this is about ?

don't tell me you forgot. !!

?

???????? Public?override?string? Message

???????? {

???????????? Get

???????????? {

???????????????? Return?base. Message;

????????????}

????????}

????}

?

}

Within this, you define the exception class myexception:applicationexception,catch( Myexception?me) user captures their own definition exceptions. catch(exception?e) is used to catch a generic exception, assuming that the exception was caught by the first catch , then the second the catch will not run and run directly in the finally statement.

?

?

Here are a few things to note about the exception classes that you define :

1. Assuming that your exception class is required to write to a file , such as a log , you need to declare the exception class as serializable [Serializable]

2. To implement a non-parameter constructor, a non-parameter exception may be thrown

3. Implement a constructor that includes a message

4. Implement a constructor that includes a message and an inner exception type

5. Add your own from the error identification data member

?

So : in general, your own defined format is this

???? Class? MyException?:? ApplicationException

???? {

???????? This is a non-participating constructor , try it .

???????? Public? MyException ()

???????? {?}

?

???????? Public? MyException (String?message)

????????????:? Base (Message)

???????? {?}

?

???????? Public? MyException (String?message,? Exception?inner)

????????????:? Base (Message,?inner)

???????? {?}

????}

?

C # Advanced Programming 82-day----user-defined exception class

Related Article

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.