fork download
  1. using System;
  2.  
  3. namespace Test
  4. {
  5. public class HttpException : Exception
  6. {
  7. public HttpException(int status)
  8. {
  9. StatusCode = status;
  10. }
  11.  
  12. public int StatusCode { get; set; }
  13. }
  14.  
  15. class Program
  16. {
  17. static void TestCatch(int status)
  18. {
  19. try
  20. {
  21. throw new HttpException(status);
  22. }
  23. catch (HttpException ex) when (ex.StatusCode == 404)
  24. {
  25. Console.WriteLine("Not Found!");
  26. }
  27. catch (HttpException ex) when (ex.StatusCode >= 500 && ex.StatusCode < 600)
  28. {
  29. Console.WriteLine("Server Error");
  30. }
  31. catch (HttpException ex)
  32. {
  33. Console.WriteLine("HTTP Error {0}", ex.StatusCode);
  34. }
  35. }
  36. static void Main(string[] args)
  37. {
  38. TestCatch(404);
  39. TestCatch(501);
  40. TestCatch(101);
  41. }
  42. }
  43. }
  44.  
Success #stdin #stdout 0.02s 15904KB
stdin
Standard input is empty
stdout
Not Found!
Server Error
HTTP Error 101