VitzzViperzz
Well-known member
Hello,
So I am following this book and we are at a point where we are covering Try...catch...finally. But I don't understand the last application we made. The code is also pretty long. I have personal never write this much code in my life. It sounds bad, this is some complicated stuff.
I do understand that we have started using the Try and catch to help minimize the exception potential as much as possible. But I really don't understand anything that the code is trying to teach.
In my eyes, we are essentially writing to test to the console and pretending that we have errors and how we would handle them. But that's about it.
Could someone just give a quick explanation?
Thanks
So I am following this book and we are at a point where we are covering Try...catch...finally. But I don't understand the last application we made. The code is also pretty long. I have personal never write this much code in my life. It sounds bad, this is some complicated stuff.
C#:
namespace Ch07Ex02
{
class Program
{
static string[] eTypes = { "none", "simple", "index",
"nested index", "filter" };
static void Main(string[] args)
{
foreach (string eType in eTypes)
{
try
{
WriteLine("Main() try block reached."); // Line 21
WriteLine($"ThrowException(\"{eType}\") called.");
ThrowException(eType);
WriteLine("Main() try block continues."); // Line 24
}
catch (System.IndexOutOfRangeException e) when (eType == "filter") // Line 26
{
BackgroundColor = ConsoleColor.Red;
WriteLine($"Main() FILTERED System.IndexOutOfRangeException catch block reached. Message:\n\"{e.Message}\"");
ResetColor();
}
catch (System.IndexOutOfRangeException e) // Line 32
{
WriteLine($"Main() System.IndexOutOfRangeException catch block reached. Message:\n\"{e.Message}\"");
}
catch // Line 36
{
WriteLine("Main() general catch block reached.");
}
finally
{
WriteLine("Main() finally block reached.");
}
WriteLine();
}
ReadKey();
}
static void ThrowException(string exceptionType)
{
WriteLine($"ThrowException(\"{exceptionType}\") reached.");
switch (exceptionType)
{
case "none":
WriteLine("Not throwing an exception.");
break; // Line 57
case "simple":
WriteLine("Throwing System.Exception.");
throw new System.Exception(); // Line 60
case "index":
WriteLine("Throwing System.IndexOutOfRangeException.");
eTypes[5] = "error"; // Line 63
break;
case "nested index":
try // Line 66
{
WriteLine("ThrowException(\"nested index\") " +
"try block reached.");
WriteLine("ThrowException(\"index\") called.");
ThrowException("index"); // Line 71
}
catch // Line 66
{
WriteLine("ThrowException(\"nested index\") general"
+ " catch block reached.");
throw;
}
finally
{
WriteLine("ThrowException(\"nested index\") finally"
+ " block reached.");
}
break;
case "filter":
try // Line 86
{
WriteLine("ThrowException(\"filter\") " +
"try block reached.");
WriteLine("ThrowException(\"index\") called.");
ThrowException("index"); // Line 91
}
catch // Line 93
{
WriteLine("ThrowException(\"filter\") general"
+ " catch block reached.");
throw;
}
break;
}
}
}
}
I do understand that we have started using the Try and catch to help minimize the exception potential as much as possible. But I really don't understand anything that the code is trying to teach.
In my eyes, we are essentially writing to test to the console and pretending that we have errors and how we would handle them. But that's about it.
Could someone just give a quick explanation?
Thanks