Better is this code which makes more sense:
namespace ConsoleApplication1
{
class myexception : Exception
{
public myexception(string str)
{
Console.WriteLine("user define exception" + str);
}
}
class Program
{
static void Main(string[] args)
{
try
{
throw new myexception("aquib");
}
catch (myexception e)
{
Console.WriteLine("myexception caught here" + e.ToString());
}
catch (Exception e)
{
Console.WriteLine("general exception caught here" + e.ToString());
}
Console.WriteLine("last statement");
Console.ReadKey();
}
}
}
Using the above code you are able to differentiate between your exception being caught or general exceptions that e.g. the .NET framework throws.
You should throw your own exceptions in case you determine in your functions that something was bad.
The program flow is as follows:The "main" function is entered and when the exception is thrown, the code that is directly after that is not executed since it jumps to the catch block that is appropriate. In the case above it jumps to "catch(myexception e)". After handling the exception it can continue with the next code. In your case it writes to the output "last statement".
Please read these articles for some more information:
Exception Handling Best Practices in .NET[
^]
User Friendly Exception Handling[
^]