Skip to content

Commit dddb03c

Browse files
Finally e using (parte 2)
1 parent 1c6976e commit dddb03c

File tree

6 files changed

+39
-26
lines changed

6 files changed

+39
-26
lines changed

TrabalhandoComExcecoes/_03_ByteBank/LeitorDeArquivo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace _03_ByteBank
55
{
6-
public class LeitorDeArquivo
6+
public class LeitorDeArquivo : IDisposable
77
{
88
public string Arquivo { get; }
99

@@ -12,7 +12,7 @@ public LeitorDeArquivo(string arquivo)
1212
Arquivo = arquivo;
1313
Console.WriteLine("Abrindo arquivo: " + arquivo);
1414

15-
throw new FileNotFoundException();
15+
//throw new FileNotFoundException();
1616
}
1717

1818
public string LerProximaLinha()
@@ -24,9 +24,9 @@ public string LerProximaLinha()
2424
//return "Linha do arquivo";
2525
}
2626

27-
public void Fechar()
27+
public void Dispose()
2828
{
29-
Console.WriteLine("Fechando arquivo");
29+
Console.WriteLine("Fechando arquivo.");
3030
}
3131
}
3232
}

TrabalhandoComExcecoes/_03_ByteBank/Program.cs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,52 @@ class Program
77
{
88
static void Main(string[] args)
99
{
10-
CarregarContas();
10+
try
11+
{
12+
CarregarContas();
13+
}
14+
catch(Exception)
15+
{
16+
Console.WriteLine("CATCH NO METODO MAIN");
17+
}
18+
1119
}
1220

1321
private static void CarregarContas()
1422
{
15-
LeitorDeArquivo leitor = null;
1623

17-
try
24+
using(LeitorDeArquivo leitor = new LeitorDeArquivo("teste.txt"))
1825
{
19-
leitor = new LeitorDeArquivo("conta.txt");
20-
2126

2227
leitor.LerProximaLinha();
23-
leitor.LerProximaLinha();
24-
leitor.LerProximaLinha();
25-
leitor.LerProximaLinha();
26-
leitor.LerProximaLinha();
27-
2828
}
29-
catch(IOException)
30-
{
31-
32-
Console.WriteLine("Exceção do tipo IOException capturada e tratada!");
3329

34-
}
35-
finally
36-
{
37-
if(leitor != null)
38-
{
39-
leitor.Fechar();
40-
}
30+
//-------------------------------------------------------
31+
32+
33+
// LeitorDeArquivo leitor = null;
34+
35+
// try
36+
// {
37+
// leitor = new LeitorDeArquivo("conta.txt");
38+
39+
40+
// leitor.LerProximaLinha();
41+
// leitor.LerProximaLinha();
42+
// leitor.LerProximaLinha();
43+
// leitor.LerProximaLinha();
44+
// leitor.LerProximaLinha();
45+
46+
// }
47+
// finally
48+
// {
49+
// Console.WriteLine("Executando o finally");
50+
// if(leitor != null)
51+
// {
52+
// leitor.Fechar();
53+
// }
4154

42-
}
55+
// }
4356

4457
}
4558

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)