Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Samuel Oliveira
lp1-2020.5
Commits
4e84c18c
Commit
4e84c18c
authored
Jul 20, 2020
by
Samuel Oliveira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Arquivos de exceções upados
parent
95689b9a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
135 additions
and
0 deletions
+135
-0
Aula 7/excecoes.cpp
Aula 7/excecoes.cpp
+49
-0
Aula 7/excecoes2.cpp
Aula 7/excecoes2.cpp
+62
-0
Aula 7/excecoes3.cpp
Aula 7/excecoes3.cpp
+24
-0
No files found.
Aula 7/excecoes.cpp
0 → 100644
View file @
4e84c18c
#include <iostream>
using
namespace
std
;
class
TrataDivZero
{
private:
int
NLinha
;
public:
TrataDivZero
(
int
n
);
void
Msg
();
};
TrataDivZero
::
TrataDivZero
(
int
n
)
{
NLinha
=
n
;
}
void
TrataDivZero
::
Msg
()
{
cout
<<
"Tratador de erro foi chamado ..."
<<
endl
;
cout
<<
"Erro na linha: "
<<
NLinha
<<
endl
;
}
int
main
(){
int
n
,
m
;
cout
<<
"Numerador: "
;
cin
>>
n
;
cout
<<
"Denominador: "
;
cin
>>
m
;
try
{
// inicio do bloco TRY
if
(
m
==
0
){
// lança a exceção, criando um objeto da classe TrataDivZero
// e passando o número da linha onde o erro ocorreu
throw
(
TrataDivZero
(
__LINE__
));
// dentro deste bloco nada é executado após o throw.
}
cout
<<
"Divisão: "
<<
(
double
)
n
/
m
<<
endl
;
}
// final do bloco TRY
catch
(
TrataDivZero
T
){
// pode ter ou não o objeto
// apenas o tipo é obrigatório
T
.
Msg
();
}
cout
<<
"Fim..."
<<
endl
;
return
0
;
}
\ No newline at end of file
Aula 7/excecoes2.cpp
0 → 100644
View file @
4e84c18c
#include <iostream>
#include <math.h>
using
namespace
std
;
class
TrataDivZero
{
private:
int
NLinha
;
public:
TrataDivZero
(
int
n
);
void
Msg
();
};
TrataDivZero
::
TrataDivZero
(
int
n
){
NLinha
=
n
;
}
void
TrataDivZero
::
Msg
(){
cout
<<
"Tratador de erro foi chamado ..."
<<
endl
;
cout
<<
"Erro na linha: "
<<
NLinha
<<
endl
;
}
int
main
(){
int
n
,
m
;
cout
<<
"Numerador: "
;
cin
>>
n
;
cout
<<
"Denominador: "
;
cin
>>
m
;
try
{
if
(
m
==
0
){
throw
(
TrataDivZero
(
__LINE__
));
cout
<<
"Problemas...."
;
// esta linha nunca é executada
}
if
(
m
<
0
){
string
temp
=
"Problema com Raiz Quadrada de número negativo."
;
throw
(
string
(
temp
));
// LANÇA UMA EXCEÇÃO COM UMA string
}
if
(
n
<
0
){
throw
(
10
);
// LANÇA UMA EXCEÇÃO COM UM int
}
cout
<<
"Divisão: "
<<
(
double
)
n
/
m
<<
endl
;
cout
<<
"Raiz Quadrada: "
<<
sqrt
(
m
)
<<
endl
;
}
catch
(
TrataDivZero
T
){
// pode ter ou não o objeto
// apenas o tipo é obrigatório
T
.
Msg
();
}
catch
(
string
S
){
// pode ter ou não o objeto
// apenas o tipo é obrigatório
cout
<<
"Tratador de exceção de string"
<<
endl
;
cout
<<
S
<<
endl
;
}
catch
(...){
cout
<<
"Tratador de exceção GENERICO !!"
<<
endl
;
}
cout
<<
"Fim..."
<<
endl
;
return
0
;
}
\ No newline at end of file
Aula 7/excecoes3.cpp
0 → 100644
View file @
4e84c18c
#include <iostream>
#include<exception>
using
namespace
std
;
int
main
(){
double
*
ptr
;
try
{
while
(
1
){
cout
<<
"Tentando Alocar...
\n
"
;
ptr
=
new
double
[
500000
];
}
}
catch
(
bad_alloc
E
){
cout
<<
"Faltou Memoria...
\n
"
<<
endl
;
}
// aqui o objeto T não existe mais !!
for
(
int
i
=
0
;
i
<
100
;
i
++
)
cout
<<
"Fim..."
<<
endl
;
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment