Exception
throw
if (aantal <= 0)
throw new ArgumentException(“Aantal moet groter zijn dan nul”);
try-catch-finally
System.IO.StreamWriter sw = null;
try
{
sw = new System.IO.StreamWriter(@"C:\test\test.txt");
sw.WriteLine("Hello");
}
catch (System.IO.FileNotFoundException ex)
{
// Put the more specific exception first.
System.Console.WriteLine(ex.ToString());
}
catch (System.IO.IOException ex)
{
// Put the less specific exception last.
System.Console.WriteLine(ex.ToString());
}
finally
{
sw.Close();
}
custom exception
[Serializable]
public class EmployeeListNotFoundException: Exception
{
public EmployeeListNotFoundException()
{
}
public EmployeeListNotFoundException(string message) : base(message)
{
}
public EmployeeListNotFoundException(string message, Exception inner) : base(message, inner)
{
}
}
filters (voegt condities toe aan catch block)
try
{
Foo.DoSomethingThatMightFail(null);
}
catch (MyException ex) when (ex.Code == 42)
{
Console.WriteLine("Error 42 occurred");
}