C # various Exception Handling Methods,

Source: Internet
Author: User

C # various Exception Handling Methods,

. NET exception handling mechanism is used to detect and handle runtime errors. If a developer does not provide an exception handling mechanism, the. NET mechanism is used by default.

 

Usually try... catch... finally to catch exceptions.

try
{
// Exceptions may occur.
}
catch(Exception ex)
{
// Handle exceptions
}
finally
{
// Clear
}

○ If no exception occurs, go directly to the finally statement block.
○ Finally statement blocks must be executed
○ The catch and finally statement blocks are optional. The try statement block can be followed by one or more catch statement blocks. The try statement block can be followed by the finally statement block.
○ Exception is the base class of all exceptions.

 

□Use the default. NET Exception Handling Mechanism to capture exceptions

    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            int result = 100/a;
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

 

□Use try... catch to manually catch exceptions

    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100/a;
Console. WriteLine ("not executed here ");
            }
            catch (DivideByZeroException exception)
            {
Console. WriteLine ("exception occurred ");
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

 

□Use try... catch... finally to manually catch exceptions

   class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100/a;
Console. WriteLine ("not executed here ");
            }
            catch (DivideByZeroException exception)
            {
Console. WriteLine ("exception occurred ");
            }
            finally
            {
Console. WriteLine ("let it go, it will definitely be executed to me ~~ ");
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

It can be seen that the content in the finally statement block will be executed.

 

□Use try... multiple catch... finally to manually catch exceptions

    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100/a;
Console. WriteLine ("not executed here ");
            }
            catch (DivideByZeroException exception)
            {
Console. WriteLine ("exceptions that cannot be divided by 0 ");
            }
            catch (Exception ex)
            {
Console. WriteLine ("exception ");
            }
            finally
            {
Console. WriteLine ("let it go, it will definitely be executed to me ~~ ");
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

It can be seen that as long as an exception is caught in a catch statement block, other catch statement blocks are not executed.

 

□Use try... catch (without parentheses or parameters) to manually catch exceptions

    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100/a;
Console. WriteLine ("not executed here ");
            }
            catch
            {
Console. WriteLine ("exception ");
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

You can catch any exceptions by using the above methods.

 

□Try... catch manually catch thrown exceptions

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
Throw new DivideByZeroException ("the divisor cannot be zero ");
            }
            catch (DivideByZeroException e)
            {
Console. WriteLine ("exception ");
            }
Console. WriteLine ("last to say ");
            Console.ReadKey();
        }
    }

The thrown exception is not displayed.

 

□Lower-level context capture throws exceptions

   class Program
    {
        static void Main(string[] args)
        {
            Calculate c = new Calculate();
            try
            {
                c.Divide();
            }
            catch (Exception e)
            {
Console. WriteLine ("Capture exceptions ");
            }
Console. WriteLine ("last to say ");
            Console.ReadKey();
        }
    }
    public class Calculate
    {
        public void Divide()
        {
            try
            {
                int a = 0;
                int result = 100/a;
            }
            catch (DivideByZeroException e)
            {
                throw;
            }
        }
    }

An exception thrown in Calculate is caught by a higher-level client.

 

□Custom exception

   class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new MyException("i am exception");
            }
            catch (Exception e)
            {
Console. WriteLine ("Custom exceptions are caught ~~ ");
            }
Console. WriteLine ("last to say ");
            Console.ReadKey();
        }
    }
    public class MyException : Exception
    {
        public MyException(string str)
        {
Console. WriteLine ("this is my custom exception:" + str );
        }
    }

 

Summary:
○. NET exception handling is not a standard try... catch... finally and can be flexible.
○ Try to throw exceptions at a lower level and capture exceptions at a higher level.


C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

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.