User-defined exceptions

Source: Internet
Author: User
Tags throw exception

User-defined exceptions

In general, it is sufficient to use exceptions provided internally by the system, but sometimes for special purposes, you must use a user-defined exception, the exception handling mechanism discussed in this article, and how to create, throw, and capture user-defined exceptions.

1. Exception handling mechanism

Whether we are using system-auto-semantic exceptions or user-defined exceptions, they all have the same exception handling mechanism, including defining exception classes, throwing exception objects, and capturing and handling exceptions, except that the first two processes are already in. NET Framework is well defined.

A) defining the exception class

There is no difference between the exception class itself and the definition of a generic class, but the exception object that is thrown and caught by using the throw keyword and the catch keyword must be a subclass object of the exception class or the exception class subclass. Therefore all user-defined exception classes must be derived from subclasses of the exception class or exception class.

b) Throw Exception object

Because an exception is an unexpected event, it does not always occur, there must be a conditional judgment statement if (satisfies the throw exception condition); and then throws the exception throw new Excrption

The following example:

if (y = = 0)//If the divisor is zero

{

throw new DivideByZeroException ();//Throw DivideByZeroException Exception class object;

}

c) Catching and handling exceptions

The catch keyword is used to catch exceptions that are thrown in a try block, depending on the list of arguments that the keyword carries, with multiple overloads, but at most one of the catch overload blocks is executed.

The following example:

catch (DivideByZeroException dz)

{

Console.WriteLine (Dz. TOSTIRNG ());

}

User-defined Exception class

All user-defined exception classes must be derived from subclasses of the exception class or exception class, so the base class that indicates the exception class must be displayed

public class myexception:exception//MyException This class is a subclass of Exception, but if it is a public class MyException: DivideByZeroException, the MyException is a subclass of the DivideByZeroException class.

You cannot override the parameterless construction method of a parent class

Add a field to MyException

string _mes; Used to save custom exception prompt information

Adding a parameterless construction method to MyException

Public MyException ()

{

Console.WriteLine ("An exception has occurred");

}

To add a parameter to MyException construction method

Public myexception (String msg)

{

_mes = msg;

Console.WriteLine ("An exception has occurred");

}

Add two arguments to myexception and indicate how to construct the two parameters of the parent class.

Public MyException (String msg,myexception E): Base (Msg,e)

{

_mes = msg;

Console.WriteLine (E.message);

Console.WriteLine ("An exception has occurred");

}

Overriding the message property of a parent class

public override string Message

{

Get

{

return _mes;

}

}

A new way to add your own exception-handling classes.

public void Mess ()

{

Console.WriteLine ("Idiot, the divisor is zero!") ");

}

In summary, the method of defining an exception class and defining a common class is consistent.

Throw User Custom exception

public class test//defines one that throws an exception object in the Div method

{

public static float div (int x,int y)//Can not use Java-like throws keywords in C # language

{

if (y = = 0)//Determine the condition that throws the exception

{

throw new MyException ("idiot, the divisor is zero!") ...);//Throws an exception object with its own defined information. \ n.//Throw A

}

Else

{

return (float) x/(float) y;

}

}

}

Catching and handling exception objects

try//all statements that may throw an exception must be placed in a try block

{

Test.div (b,a);///The code after the exception of the statement will discard execution;

}

catch (myexception me)

{

Console.WriteLine (Me. Message);

throw new MyException (me. MESSAGE,ME);

}

catch (DivideByZeroException dz)

{

}

catch (Exception ex)

{

Console.WriteLine (ex. ToString ());//After catching the exception object, you can call any method of the object.

}

finally//in general, the finally code block will be executed

{

Console.WriteLine ("finish!");

}

User-defined exceptions

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.