Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Ficha7
- {
- class Program
- {
- static void perceboAsExcepcoes()
- {
- Console.WriteLine("Teste diferentes tipos de excepções\n\n");
- string[] tipoExcepcaoOcorrida = new string[] {" Nenhuma "};
- try //Try 1
- {
- Console.WriteLine("1 - Try 1 - Antes Try 2 \n");
- try//Try 2
- {
- Console.WriteLine("2 - Try 2 - Antes Teste\n");
- /*Teste 5*/ // throw new System.IO.FileNotFoundException();
- /*Teste 4*/ // throw new System.IndexOutOfRangeException();
- /*Teste 3*/ // throw new System.IO.DirectoryNotFoundException();
- /*Teste 2*/ // throw new System.FormatException("A minha mensagem");
- /*Teste 1*/ // throw new System.DivideByZeroException();
- Console.WriteLine("3 - Try 2 - Depois do Teste\n");
- }
- catch (DivideByZeroException e)
- {
- Console.WriteLine("4 - Try 2 - Catch(DivideByZeroException) \n");
- tipoExcepcaoOcorrida = e.ToString().Split(':');
- }
- catch (FormatException e)
- {
- Console.WriteLine("5 - Try 2 - Catch(FormatException) \n");
- tipoExcepcaoOcorrida = e.ToString().Split(':');
- }
- catch (DirectoryNotFoundException e)
- {
- Console.WriteLine("6 - Try 2 - Catch(DirectoryNotFoundException) \n");
- tipoExcepcaoOcorrida = e.ToString().Split(':');
- }
- catch (IOException e)
- {
- Console.WriteLine("7 - Try 2 - Catch(IOException) - Antes Throw - mensagem: {0} \n", e.Message);
- tipoExcepcaoOcorrida = e.ToString().Split(':');
- throw;
- Console.WriteLine("Será que vou ver esta mensagem ??? Nunca !!! \n");
- }
- Console.WriteLine("8 - Try 1 - Depois do Try 2 \n");
- }
- catch (Exception e)
- {
- tipoExcepcaoOcorrida = e.ToString().Split(':');
- Console.WriteLine("\n\n\n");
- Console.WriteLine("= 9 ============== Try Catch Externo ===============");
- Console.WriteLine("\n\tMessage: " + e.Message);
- Console.WriteLine("\n\tSource: " + e.Source);
- Console.WriteLine("\n\tStackTrace: " + e.StackTrace);
- Console.WriteLine("\n\tTargetSite: " + e.TargetSite);
- Console.WriteLine("\n\tToString: " + e.ToString()); Console.WriteLine("===============================================\n\n\n");
- }
- finally
- {
- Console.WriteLine("\n\n\n");
- Console.WriteLine("10 - Try 1 - finally - aqui passo sempre!!!\t\tExcepção que ocorreu:\t{0}\n", tipoExcepcaoOcorrida[0]);
- }
- }
- static void Main()
- {
- perceboAsExcepcoes();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement