Exception-throwing capture mechanism in C #--throw/try,catch,finally

Source: Internet
Author: User
Tags finally block try catch

Try {   MessageBox.Show ("true");} Catch {   MessageBox.Show ("false");} finally {   MessageBox.Show ("finally");}

Notes

Throw exception with throw new exception, catch exception with try: Catch.. Finally

    The
    • Try ... catch is intended to resolve an issue in which the program cannot continue execution in the event of an error. The
    • try does not have to be paired with a catch or can be try{}finally{}, which is not meaningless because it guarantees that the code in the finally will be executed even if an exception occurs. Sometimes, this is very useful. The
      can be used, for example, to free up some of the resources it consumes, and then let the caller handle the exception. The
    • capture of exceptions consists of three parts:
    •  try   //  code executed, which may have an exception. Once an exception is found, jump to catch execution immediately. Otherwise, the contents of the catch are not executed   " catch   { //  Unless an exception occurs in the execute code in the try, the code here does not execute    " finally   //  Whatever happens, including a return in the try Catch, It is understood that the finally } 

        must be executed whenever a try or catch is executed;

Try Statement

Catch statement

Finally statement

Try--meaning "give it a Try"
Catch--meaning "catch", catch one.
If there is no mistake, you cannot catch the wrong. Mistakes can be caught and processed.

    • A try can be followed by any catch(the number is unlimited), mainly to see how many exception types, including the custom, Eeception is a class, declare an ex Eeception object, This ex contains all the exception information that currently occurs in the captured try block, such as ex. Message most commonly used (the detailed exception information for a try block),
    • Ex position: Only in Cath block (Exception handling block)
      The role of ex: preserves system-caught anomalies;
      Why use ex: because you're not God, you can't predict what's wrong with your program, so use
      MessageBox.Show (ex. message), let the system prompt you the program is wrong, you can go to the clues, catch the source of the error to solve him!
    • If a catch statement captures an exception that cannot be thrown by a try statement, the code cannot pass if the Catch statement uses the following method: catch (Exception e) {} will be compiled in any case.
•try{    dataconnection.open ();    Datacommand.executereader ();    ...     return ;} finally {    dataconnection.close ();}


The finally statement block will always execute, whether or not an exception is thrown, or return from somewhere in the catch, so that you have the opportunity to call close to close the database connection (even if it is not open or open, the shutdown is always possible), so that the resulting connection can be freed. Frees resources.
It is also stated that return can be placed in a try statement block. But no matter when the time comes to return, the finally will be executed before returning.

    • The function of throw is to make the program error when the code executes here, and the reason for the error is that the content you specified
      Catch is the role of the try contains the contents of the run error, catch to catch a try inside the cause of the error, as for this error you how to put in the catch, you can write the error log, MessageBox output and so on

=========================================================================

  • Try
    Catch
    Finally
    1. The code that anticipates the possibility of throwing an exception is included in the TRY statement block.
    2. If an exception occurs, the execution of the catch is transferred. Catch is written in several ways:
    Catch
    This will catch any exceptions that occur.
    catch (Exception e)
    This will catch any exceptions that occur. In addition, the E parameter is provided, and you can use the e parameter to obtain information about the exception when handling the exception.
    catch (derived class E of exception)
    This captures the exceptions defined by the derived class, for example, I want to catch an exception for an invalid operation, which can be written as follows:
    catch (InvalidOperationException e)
    {
    ....
    }
    This way, if the exception thrown in the Try statement block is InvalidOperationException, it will go to that place and the other exception is not handled.
    --------------------------------------------------------------------------------------------------------------- -----------
    Catch can have more than one, or no, each catch can handle a specific exception.. NET finds exception handling blocks in the order in which you catch them, and if found, handles them and throws up a level if they are not found. If there is no previous level, then thrown to the user, at this point, if you are debugging, the program will break run, if the program is deployed, will be aborted.
    If there is no catch block, the exception is always thrown to the upper (if any), or the program is interrupted.

    3. Finally
    Finally, there can be no or only one. Whether or not an exception occurs, it always runs at the end of this exception handling structure. Even if you return with return within a try block, finally is always executed before returning, which gives you a chance to do some cleanup at the end of the exception handling. such as closing the database connection, and so on.
    Note: If there is no catch statement block, then the finally block is required.

    If you do not want to handle the exception here, and when the exception occurs, commit to the upper layer processing, but in this place no matter what happens, you have to do something, you can use try Finally,
    A typical application is to perform database operations:
    Use the following primitive to illustrate:
    Try
    {
    Dataconnection.open ();
    Datacommand.executereader ();
    ...
    Return
    }
    Finally
    {
    Dataconnection.close ();
    }

    The finally statement block will always execute, whether or not an exception is thrown, or return from any place, so that you have the opportunity to call close to close the database connection (even if it is not open or open, the shutdown is always possible), so that the resulting connection can be freed and the resource freed.

    By the way, return can be placed in a try statement block. But no matter when the time comes to return, the finally will be executed before returning.


Summary
try {//execute code, where there may be an exception. Once an exception is found, jump to catch execution immediately. Otherwise, the contents of the catch will not be executed}

catch {//unless the execute code inside the try has an exception, the code here does not execute}

finally {//No matter what happens, including a return in the try catch, it is understood that if a try or catch is executed, it will be executed finally}

Exception-throwing capture mechanism in C #--throw/try,catch,finally

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.