26.08.2014 Views

C# e Orientação a Objetos

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXCEPTIONS 126<br />

2 Crie uma classe para modelar os funcionários do sistema do banco.<br />

1 using System ;<br />

2<br />

3 class Funcionario<br />

4 {<br />

5 private double salario ;<br />

6<br />

7 public void AumentaSalario ( double aumento )<br />

8 {<br />

9 if( aumento < 0)<br />

10 {<br />

11 ArgumentException erro = new ArgumentException ();<br />

12 throw erro ;<br />

13 }<br />

14 }<br />

15 }<br />

Código <strong>C#</strong> 13.7: Funcionario.cs<br />

3 Agora teste a classe Funcionario.<br />

1 class TestaFuncionario<br />

2 {<br />

3 static void Main ()<br />

4 {<br />

5 Funcionario f = new Funcionario ();<br />

6 f. AumentaSalario ( -1000) ;<br />

7 }<br />

8 }<br />

Código <strong>C#</strong> 13.8: TestaFuncionario.cs<br />

Execute e observe o erro no console.<br />

4 Altere o teste para capturar o erro.<br />

1 using System ;<br />

2<br />

3 class TestaFuncionario<br />

4 {<br />

5 static void Main ()<br />

6 {<br />

7 Funcionario f = new Funcionario ();<br />

8<br />

9 try<br />

10 {<br />

11 f. AumentaSalario ( -1000) ;<br />

12 }<br />

13 catch ( ArgumentException e)<br />

14 {<br />

15 System . Console . WriteLine (" Houve uma ArgumentException<br />

16 ao aumentar o salário ");<br />

17 }<br />

18 }<br />

19 }<br />

Código <strong>C#</strong> 13.9: TestaFuncionario.cs<br />

126 www.k19.com.br

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!