This lesson defines the scope statement and explains how it is different from catch and finally.

We'll cover the following

scope #

As we have seen in the previous chapter, expressions that must always be executed are written in the finally block, and expressions that must be executed when there are error conditions are written in catch blocks.
We can make the following observations about the use of these blocks:

  • catch and finally cannot be used without a try block.

  • Some of the variables that these blocks need may not be accessible within these blocks:

    void foo(ref int r) {
        try {
            int addend = 42; 
            r += addend;
        } catch (Exception exc) {
            r -= addend;// ← compilation ERROR

Get hands-on with 1200+ tech skills courses.