Blog

Filter posts by Category Or Tag of the Blog section!

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

Friday, 21 February 2014

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:

 

            if (a == b)
                Console.WriteLine("");
            else
                Console.WriteLine("");

 

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

 

           if (a == b)
            {
                Console.ReadKey();
                Console.WriteLine("");
            }
            else if (a != b)
            {
                Console.ReadKey();
                Console.WriteLine("");
                Console.WriteLine("");
            }

 

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

 

   if (a == b)
            {
                if (b < a)
                {
                    Console.ReadKey();
                    Console.WriteLine("");
                  
                    if (b < a + c)
                        Console.WriteLine("");
                    else
                        Console.WriteLine("");
                }
                else
                {
                    Console.Read();
                    Console.WriteLine("");
                }
            }
            else
            {
                Console.ReadKey();
                Console.WriteLine("");
            }

 

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 a 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:

 

           try
            {
                if (a == b)
                    Console.WriteLine();
            }
            catch (ArgumentException argumentException)
            {
                Console.WriteLine(argumentException.Message);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

 

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

 

          try   
                if (a == b)
                    Console.WriteLine();
           
            catch (ArgumentException argumentException)    
                Console.WriteLine(argumentException.Message);
       
           catch (Exception exception)
                Console.WriteLine(exception.Message);
           

 

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

 

            try   
                if (a == b)
                    Console.WriteLine();
            try   
                if (b== c)
                    Console.WriteLine();
           
            catch (ArgumentException argumentException)    
                Console.WriteLine(argumentException.Message);

            catch (ArgumentException argumentException)    
                Console.WriteLine(argumentException.Message);
       
            catch (Exception exception)
                Console.WriteLine(exception.Message);

 

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 bracketless try/catch because the compiler only detects them by the bracket to determine that which cache is for which try, take another complex nested example:

 

           try
            {
                try
                {
                    try
                    {
                        if (a == b)
                            Console.WriteLine("");
                    }
                    catch (Exception exception)
                    {
                        try
                        {
                            //try something
                        }
                        catch (Exception exception2)
                        {
                            Console.Write(exception2.Message);
                        }
                        Console.WriteLine(exception.Message);
                    }
                }
                catch (Exception e)
                {
                    Console.Write(e.Message);
                }
              
            }
            catch (ArgumentException argumentException)
            {
                Console.WriteLine(argumentException.Message);
            }
            catch (Exception exception)
            {
                try
                {
                    //try somthing
                }
                catch (Exception ee)
                {
                    Console.WriteLine(ee.Message);
                }
                Console.WriteLine(exception.Message);
            }

 

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

 

 

Category: Software

Tags: C#

comments powered by Disqus