Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace Ficha7
  8. {
  9. class Program
  10. {
  11. static void perceboAsExcepcoes()
  12. {
  13. Console.WriteLine("Teste diferentes tipos de excepções\n\n");
  14. string[] tipoExcepcaoOcorrida = new string[] {" Nenhuma "};
  15. try //Try 1
  16. {
  17. Console.WriteLine("1 - Try 1 - Antes Try 2 \n");
  18. try//Try 2
  19. {
  20. Console.WriteLine("2 - Try 2 - Antes Teste\n");
  21. /*Teste 5*/ // throw new System.IO.FileNotFoundException();
  22. /*Teste 4*/ // throw new System.IndexOutOfRangeException();
  23. /*Teste 3*/ // throw new System.IO.DirectoryNotFoundException();
  24. /*Teste 2*/ // throw new System.FormatException("A minha mensagem");
  25. /*Teste 1*/ // throw new System.DivideByZeroException();
  26. Console.WriteLine("3 - Try 2 - Depois do Teste\n");
  27. }
  28. catch (DivideByZeroException e)
  29. {
  30. Console.WriteLine("4 - Try 2 - Catch(DivideByZeroException) \n");
  31. tipoExcepcaoOcorrida = e.ToString().Split(':');
  32. }
  33. catch (FormatException e)
  34. {
  35. Console.WriteLine("5 - Try 2 - Catch(FormatException) \n");
  36. tipoExcepcaoOcorrida = e.ToString().Split(':');
  37. }
  38. catch (DirectoryNotFoundException e)
  39. {
  40. Console.WriteLine("6 - Try 2 - Catch(DirectoryNotFoundException) \n");
  41. tipoExcepcaoOcorrida = e.ToString().Split(':');
  42. }
  43. catch (IOException e)
  44. {
  45. Console.WriteLine("7 - Try 2 - Catch(IOException) - Antes Throw - mensagem: {0} \n", e.Message);
  46. tipoExcepcaoOcorrida = e.ToString().Split(':');
  47. throw;
  48. Console.WriteLine("Será que vou ver esta mensagem ??? Nunca !!! \n");
  49. }
  50. Console.WriteLine("8 - Try 1 - Depois do Try 2 \n");
  51. }
  52. catch (Exception e)
  53. {
  54. tipoExcepcaoOcorrida = e.ToString().Split(':');
  55. Console.WriteLine("\n\n\n");
  56. Console.WriteLine("= 9 ============== Try Catch Externo ===============");
  57. Console.WriteLine("\n\tMessage: " + e.Message);
  58. Console.WriteLine("\n\tSource: " + e.Source);
  59. Console.WriteLine("\n\tStackTrace: " + e.StackTrace);
  60. Console.WriteLine("\n\tTargetSite: " + e.TargetSite);
  61. Console.WriteLine("\n\tToString: " + e.ToString()); Console.WriteLine("===============================================\n\n\n");
  62. }
  63. finally
  64. {
  65. Console.WriteLine("\n\n\n");
  66. Console.WriteLine("10 - Try 1 - finally - aqui passo sempre!!!\t\tExcepção que ocorreu:\t{0}\n", tipoExcepcaoOcorrida[0]);
  67. }
  68. }
  69.  
  70.  
  71. static void Main()
  72. {
  73. perceboAsExcepcoes();
  74. Console.ReadKey();
  75.  
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement