Generic catch Statements
C# also supports a “generic” catch scope that does not explicitly receive the exception object
thrown by a given member:
// A generic catch.
static void Main(string[] args)
{
Console.WriteLine("***** Handling Multiple Exceptions *****\n");
Car myCar = new Car("Rusty", 90);
try
{
myCar.Accelerate(90);
}
catch
{
Console.WriteLine("Something bad happened...");
}
Console.ReadLine();
}
Obviously, this is not the most informative way to handle exceptions, given that you have no
way to obtain meaningful data about the error that occurred (such as the method name, call stack,
or custom message). Nevertheless, C# does allow for such a construct, which can be helpful when
you wish to handle all errors in a very generic fashion.
Do'stlaringiz bilan baham: |