Skip to content

Commit 9cc8e09

Browse files
ArgumentException ParamName and nameOf
1 parent 95108e2 commit 9cc8e09

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

TrabalhandoComExcecoes/_03_ByteBank/ContaCorrente.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1+
using System;
2+
13

24
namespace _03_ByteBank
35
{
46
public class ContaCorrente
57
{
68
public static double TaxaOperacao { get; private set; }
79
public Cliente Titular{ get; set; }
8-
910
public static int TotalDeContasCriadas{get; private set;}
10-
private int _agencia;
11-
public int Agencia
12-
{
13-
get
14-
{
15-
return _agencia;
16-
}
17-
set
18-
{
19-
_agencia = value;
20-
}
21-
}
22-
public int Numero{get; set;}
11+
public int Agencia {get;}
12+
public int Numero{get;}
2313
private double _saldo = 100;
2414
public double Saldo
2515
{
@@ -38,6 +28,16 @@ public double Saldo
3828
}
3929
public ContaCorrente(int agencia, int numero)
4030
{
31+
32+
if(agencia <= 0)
33+
{
34+
throw new ArgumentException("O argumento Agencia não é maior que 0", nameof(agencia));
35+
}
36+
37+
if(numero <= 0)
38+
{
39+
throw new ArgumentException("O argumento Número não é maior que 0", nameof(numero));
40+
}
4141
Agencia = agencia;
4242
Numero = numero;
4343

TrabalhandoComExcecoes/_03_ByteBank/Program.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,24 @@ static void Main(string[] args)
88
{
99
try
1010
{
11-
Metodo();
11+
ContaCorrente conta = new ContaCorrente(667, 98887);
12+
}
13+
catch(ArgumentException ex)
14+
{
15+
if(ex.ParamName == "numero")
16+
{
17+
18+
}
19+
Console.WriteLine("Argumento com problema " + ex.ParamName);
20+
Console.WriteLine("Ocorreu uma exceção do tipo ArgumentException");
21+
Console.WriteLine(ex.Message);
1222
}
1323
catch(Exception ex)
1424
{
1525
Console.WriteLine(ex.Message);
16-
Console.WriteLine(ex.StackTrace);
1726
}
27+
28+
//Metodo();
1829
}
1930

2031
static void Metodo()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)