1

Go through the below code

Example:

try
{
    //some code is executing..
    //1.some SqlException thrown
    //2.some FormatException thrown
    //3. other Exception thrown
}
catch(SqlException sqlex)
{
    Console.WriteLine("sqlexception is returned");
}
catch(FormatException fx)
{
    Console.WriteLine("FormatException is returned");
}
catch(Exception ex)
{
    Console.WriteLine("Mainexception is returned");
}
catch
{
    Console.WriteLine("exception without any args is returned");
}
  1. what might be the output of this, and why.?

  2. which catch block will be executed at first and why?

  3. If i declare catch(Exception ex)-immediately after the try block then it will not compile and gives an error for other catch blocks -"a previous catch clause catches all the exceptions" - so does this catch block with argument System.Exception acts as a master exception or main exception block..? if so why..?

Please advise and thanks for the help in advance.

  • Very good descriptions are available on MSDN: [Using Exceptions](http://msdn.microsoft.com/en-us/library/ms173161.aspx) and several other topics in [Exceptions and Exception Handling](http://msdn.microsoft.com/en-us/library/ms173160.aspx) – Jim Mischel Aug 23 '13 at 13:41

4 Answers4

2

Putting things in simple words: The catch block works sequentially. i.e. which ever catch block is caught as per the matching exceptions defined. e.g. if you define a generic exception catch block at top then it will catch all types of exceptions and other catch blocks will never be called. so in your example as Karl defined 4th catch block will never be called.

Gautam
  • 1,728
  • 8
  • 32
  • 67
1

The first catch that meets the criteria (this criteria also includes exception types that inherit from the type too) for the type of exception thrown will execute. This is why you put more specific exception types earlier in the list and the most generic exception types at the bottom.

In your example, the last catch will never execute, because the catch(Exception ex) above it will catch all exception types, as every exception is based upon the System.Exception class.

So from the code you posted, the only guarantee is the last one will never execute if you are using strictly .NET code, it is possible for an exception to generated outside of .NET that does not inherit from System.Exception. For all the others, it depends upon the type of exception as to which one will actually execute.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • thanks for the answer,i re-edited my question to be more specific,so if am not wrong, if the compiler doesn't finds the specific exception type in my code , then it will fall to the main exception i.e, System.Exception, right.? – Prathap S V Genny Aug 23 '13 at 13:10
  • The last catch *can* execute: it will catch any exceptions that don't inherit from System.Exception; for example, any thrown by code that isn't part of .net. See http://stackoverflow.com/questions/10805987/catch-vs-catch-exception-e-and-throw-vs-throw-e – Adrian Wragg Aug 23 '13 at 13:48
  • @AdrianWragg - okay, that is true, but highly unlikely in this scenario. – Karl Anderson Aug 23 '13 at 13:53
0

The reason for having multiple catch statements is that is allows you to have handlers for different types of Exceptions. So in your example:

  catch(SqlException sqlex)
  catch(FormatException fx)
  catch(Exception ex)
  catch
  • If the code triggers a SqlException (or any exception that inherits from it), then the code within that will run.
  • If a FormatException, then the code in that block.
  • All other exceptions derived from the base Exception class, the third will run.
  • Anything else (primarily errors thrown by non-.net code), the fourth.

The reason why the code will not compile if the third is moved is that all the built-in exceptions are subclasses of Exception itself; if Exception is handled first then those handlers can never run.

Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50
  • @PrathapSVGenny If one of these answers - doesn't have to be mine, won't be offended - answers your question, it's worth accepting it so other people with similar questions can see what was of use to you. – Adrian Wragg Aug 23 '13 at 13:44
0
            try
            {
                //some code is executing..

            }
            catch(SqlException sqlex)
            {
                 // sql exception goes here
                Console.WriteLine("sqlexception is returned");
             }
            catch (FormatException fx)
            {
                //format exception goes here
                Console.WriteLine("FormatException is returned");
            }
            catch (Exception ex)
            {
                 // Any other or general exception goes here
                Console.WriteLine("Mainexception is returned");
            }
            catch
            {
                 //none of the exceptions for this catch
                Console.WriteLine("exception without any args is returned");
            }
manivannan
  • 622
  • 1
  • 4
  • 17