Why brackets are required for try/catch in C# ?

Have you ever think about that!? You don’t need to put bracket statement of if/else condition if your statement is not multiline. Needless to say, if you want to put if/else condition with just one line statement, it would be something like this:

  1.             if (a == b)
  2.                 Console.WriteLine("");
  3.             else
  4.                 Console.WriteLine("");

 

And if you have multiline statement for your if/else condition:

  1.            if (a == b)
  2.             {
  3.                 Console.ReadKey();
  4.                 Console.WriteLine("");
  5.             }
  6.             else if (a != b)
  7.             {
  8.                 Console.ReadKey();
  9.                 Console.WriteLine("");
  10.                 Console.WriteLine("");
  11.             }

 

Right? So let's dig in deeper, if you have nested if/else like this:

  1.    if (a == b)
  2.             {
  3.                 if (b < a)
  4.                 {
  5.                     Console.ReadKey();
  6.                     Console.WriteLine("");
  7.                   
  8.                     if (b < a + c)
  9.                         Console.WriteLine("");
  10.                     else
  11.                         Console.WriteLine("");
  12.                 }
  13.                 else
  14.                 {
  15.                     Console.Read();
  16.                     Console.WriteLine("");
  17.                 }
  18.             }
  19.             else
  20.             {
  21.                 Console.ReadKey();
  22.                 Console.WriteLine("");
  23.             }

 

That wouldn't make any difference because in C#, there is no any else without if and also there is totally one else for every if! It means that compiler will look for your if and will assign the next else to it, if you have bracket or not.

But in try/catch you can have multiple catches for a try and a try without any catch is impossible (because you should catch what have you tried!), take a look at this:

  1.            try
  2.             {
  3.                 if (a == b)
  4.                     Console.WriteLine();
  5.             }
  6.             catch (ArgumentException argumentException)
  7.             {
  8.                 Console.WriteLine(argumentException.Message);
  9.             }
  10.             catch (Exception exception)
  11.             {
  12.                 Console.WriteLine(exception.Message);
  13.             }

 

Now imagine this try/catch without any brackets (although it's a compile error!)

  1.           try   
  2.                 if (a == b)
  3.                     Console.WriteLine();
  4.            
  5.             catch (ArgumentException argumentException)    
  6.                 Console.WriteLine(argumentException.Message);
  7.        
  8.            catch (Exception exception)
  9.                 Console.WriteLine(exception.Message);
  10.            

 

Now, if you put another try/catch (bracket less) in this try , what will happen?       

  1.             try   
  2.                 if (a == b)
  3.                     Console.WriteLine();
  4.             try   
  5.                 if (b== c)
  6.                     Console.WriteLine();
  7.            
  8.             catch (ArgumentException argumentException)    
  9.                 Console.WriteLine(argumentException.Message);
  10.  
  11.             catch (ArgumentException argumentException)    
  12.                 Console.WriteLine(argumentException.Message);
  13.        
  14.             catch (Exception exception)
  15.                 Console.WriteLine(exception.Message);
  16.  

 

Which one of the above catches is for the first try? And which one of them is for the second try?? As every try has at least one catch so we have two try with three catch, witch one of them has two catches!??

That's why that it's impossible to have a bracket less try/catch because the compiler only detects them by bracket to determine that which catche is for which try, take another complex nested example:

  1.            try
  2.             {
  3.                 try
  4.                 {
  5.                     try
  6.                     {
  7.                         if (a == b)
  8.                             Console.WriteLine("");
  9.                     }
  10.                     catch (Exception exception)
  11.                     {
  12.                         try
  13.                         {
  14.                             //try something
  15.                         }
  16.                         catch (Exception exception2)
  17.                         {
  18.                             Console.Write(exception2.Message);
  19.                         }
  20.                         Console.WriteLine(exception.Message);
  21.                     }
  22.                 }
  23.                 catch (Exception e)
  24.                 {
  25.                     Console.Write(e.Message);
  26.                 }
  27.               
  28.             }
  29.             catch (ArgumentException argumentException)
  30.             {
  31.                 Console.WriteLine(argumentException.Message);
  32.             }
  33.             catch (Exception exception)
  34.             {
  35.                 try
  36.                 {
  37.                     //try somthing
  38.                 }
  39.                 catch (Exception ee)
  40.                 {
  41.                     Console.WriteLine(ee.Message);
  42.                 }
  43.                 Console.WriteLine(exception.Message);
  44.             }

 

I have market that catches of every try and Compiler will detect the catches only by bracket scopes, got it!? hope it be useful…

 

 


Tags: C#


comments powered by Disqus