Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when we have multiple catch for a single try block? and why ? 


What I have tried:

<pre>
try
{
//some code
}
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");
}
Posted
Updated 16-Aug-18 6:20am

They fall through so the first one (from the top) that matches the Exception type will be executed and the others will not be.

Also, your last catch will never be hit.

You can prove this by running:
C#
void Main()
{
	try
	{
		throw new Exception();
	}
	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");
	}
}


Here's another example you can try. It will print "invalid" because the InvalidOperationException is thrown upon attempt to open the invalid connection.

void Main()
{
	try
	{
		SqlConnection conn = new SqlConnection("");
		conn.Open();
	}
	catch(SqlException sqlex)
	{
		Console.WriteLine("sqlexception is returned");
	}
	catch (InvalidOperationException iox)
	{
		Console.WriteLine("invalid");
	}
	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");
	}
}


However, if you removed the InvalidOperationException catch then it would fall down to "Mainexception is returned".
 
Share this answer
 
v3
Just to add up to the existing solution provided...

Quote:
Which catch block is executed first when we have multiple catch for a single try block?


Depending on the type of exception your application code generates. Since you were catching a few Exception types then:

(a) When your SQL Server throws a warning or error then the SqlException catch block get's executed.
(b) When you are trying to convert something that is not well formed, or the format of an argument is invalid then the FormatException catch block gets executed.
(c) When a null reference or invalid operation occurred then it will goes to the Exception catch block since you didn't specifically handle NullReferenceException and InvalidOperationException. The Exception type is typically used to handle generic exceptions.

Quote:
and why ?


Multiple Catch block are typically used to handle different kinds of Exception. Just like in your example: SqlException, FormatException and Exception types (keep in mind that there are many types of Exceptions depending on what operation you are trying to do).

Exceptions are a type of error that occurs during the execution of an application. Errors are typically problems that happens unexpctedly. Whereas, Exceptions are expected to happen within application code that's why it is important to handle expected exceptions and catch them specifically to easily identify which of which causes the exception to happen. Handling specific exceptions is very useful when doing some logging and allows an application to transfer control from one part of the code to another.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900