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 , so let's talk about the why did of custom exceptions .

Why does a custom exception class appear ? use your toes to think and understand . , is to define our own anomalies. , Custom exception classes inherit from ApplicationException

The first exception (Exception) is divided into two categories , one of which is the predefined exception class , which is the class that needs to be inherited when the user customizes the exception class .

Case :

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

Namespace Custom Exception classes

{

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 see 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) {}// did you know it was a dry word ? don't tell me you forgot. !!

public override string Message

{

Get

{

Return base. Message;

}

}

}

}

In this custom exception class myexception:applicationexception,catch( MyException me) user captures a custom exception,catch(Exception e) is used to catch a generic exception if the exception is first catch capture, the second catch will not execute, directly execute The statement in finally.

Here are a few things to note about custom exception classes :

1. If 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 parameterless constructor , because a parameterless exception may be thrown

3. Implement a constructor that contains a message

4. Implement a constructor that contains the message and the inner exception type

5. Add your own from the error identification data member

So , in general, the custom format is like this

Class Myexception:applicationexception

{

This is a non-parametric constructor , try it .

Public MyException ()

{ }

Public myexception (String message)

: Base (Message)

{ }

Public myexception (String message, Exception inner)

: Base (message, inner)

{ }

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.