www.MarkDawson.org

Mark Dawson's place in cyber space

Home Software Fun Family About Contact
C# Software - Try/Catch/Finally quiz
The basic premise of the try/catch/finally statement is to allow code to execute inside the try statement and any exceptions to be safely caught in the catch statement.  The finally part is always executed.  Try to figure out what would be printed to the console, see what score you get from below (this is just a bit of fun :-)) - the answers can be found here.

1.

try
{
Console.WriteLine("try");
}
catch
{
Console.WriteLine("catch");
}
finally
{
Console.WriteLine("finally");
}

 

2.

try
{
Console.WriteLine("try");
          throw new Exception();
}
catch
{
Console.WriteLine("catch");
}
finally
{
Console.WriteLine("finally");
}
 
Console.WriteLine("after finally");

 

3.

try
{
Console.WriteLine("try");
          throw new Exception();
}
catch
{
Console.WriteLine("catch");
          return;
}
finally
{
Console.WriteLine("finally");
}
 
Console.WriteLine("after finally");

 

4.

try
{
Console.WriteLine("try");
          throw new Exception();
}
catch
{
Console.WriteLine("catch");
}
finally
{
Console.WriteLine("finally");
          return;
}
 
Console.WriteLine("after finally");

 

5.

try
{
Console.WriteLine("try");
        return;
}
catch
{
Console.WriteLine("catch");
}
finally
{
Console.WriteLine("finally");
}
 
Console.WriteLine("after finally");

 

6.

try
{
Console.WriteLine("try");
        throw new Exception();
}
finally
{
Console.WriteLine("finally");
}
 
Console.WriteLine("after finally");

 

7.

public void FunctionA()
{
 
      try
     {
      Console.WriteLine("try:FunctionA");
             FunctionB();
      }
       catch
      {
      Console.WriteLine("catch:FunctionA");
      }
       finally
      {
      Console.WriteLine("finally:FunctionA");
      }
}
public void FunctionB()
{
 
      try
     {
      Console.WriteLine("try:FunctionB");
               throw new Exception();
      }
       catch
      {
      Console.WriteLine("catch:FunctionB");
      }
       finally
      {
      Console.WriteLine("finally:FunctionB");
      }
 
      Console.WriteLine("after finally:FunctionB");
}

 

8.

public void FunctionA()
{
 
      try
     {
      Console.WriteLine("try:FunctionA");
             FunctionB();
      }
       catch(ExceptionBCatch)
      {
      Console.WriteLine("BCatch:FunctionA");
      }
       catch(ExceptionBFinally)
      {
      Console.WriteLine("BFinally:FunctionA");
      }
       finally
      {
      Console.WriteLine("finally:FunctionA");
      }
}
public void FunctionB()
{
 
      try
     {
      Console.WriteLine("try:FunctionB");
               throw new Exception();
      }
       catch
      {
      Console.WriteLine("catch:FunctionB");
                   throw new ExceptionBCatch();
      }
       finally
      {
      Console.WriteLine("finally:FunctionB");
                   throw new ExceptionBFinally();
      }
 
      Console.WriteLine("after finally:FunctionB");
}

 

 

Let's add a little twist and throw in a thread into the mix.  When you abort a thread a ThreadAbortException is raised, but does it behave like any other exception?

For the problem below assume that ThreadStartingMethod() is the starting point of the thread and that the Abort() method is called on the thread whilst it is asleep inside the while loop i.e. the while loop is only entered once.

9.

public void ThreadStartingMethod()
{
      try
     {
      while(true)
            {
            Console.WriteLine("try");
                   Thread.Sleep(1000000);
            }
      }
       catch
      {
      Console.WriteLine("catch");
      }
       finally
      {
      Console.WriteLine("finally");
      }
 
      Console.WriteLine("after finally");
 
}

 

10.

public void ThreadStartingMethod()
{
      try
     {
      while(true)
            {
            Console.WriteLine("try:ThreadStart");
                   FunctionB();
            }
      }
       catch
      {
      Console.WriteLine("catch:ThreadStart");
      }
       finally
      {
      Console.WriteLine("finally:ThreadStart");
      }
 
      Console.WriteLine("after finally:ThreadStart");
 
}
 
public void FunctionB()
{
 
      try
     {
      Console.WriteLine("try:FunctionB");
                   Thread.Sleep(100000);
      }
       catch
      {
      Console.WriteLine("catch:FunctionB");
      }
       finally
      {
      Console.WriteLine("finally:FunctionB");
      }
 
      Console.WriteLine("after finally:FunctionB");
}