Exception lost after rethrow
up vote
1
down vote
favorite
Good morning, guys!
I have a method ValidarConexaoEPI
wich validade a connection string trying to open a connection with it. In case of exceptions, the method who had call the "validate" one SolicitarNovaSincronizacao
, catchs it and rethrow a manipulated exception.
public static SincronizacaoSinc SolicitarNovaSincronizacao(int? idUsuario, int idAlmoxarifado, string versaoAplicacao, bool validaSincronizacaoEmAndatmento = true)
{
SincronizacaoSinc sincronizacaoSinc = null;
using (var conexaoLocal = new SqlConnectionEPI(_DbLocalConnectionString))
using (var conexaoServidor = new SqlConnectionEPI(_DbOnlineConnectionString))
{
sincronizacaoSinc = new SincronizacaoSinc(idUsuario, idAlmoxarifado, versaoAplicacao);
try
{
if (UtilDataAccess.ValidarConexaoEPI(conexaoLocal) && UtilDataAccess.ValidarConexaoEPI(conexaoServidor))
{
ValidarAlmoxarifadoBaseOnline(conexaoServidor, idAlmoxarifado);
ValidarUsuarioBaseOnline(conexaoServidor, idUsuario);
CriarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
if(validaSincronizacaoEmAndatmento)
{
SolicitarSincronizacao(conexaoServidor, sincronizacaoSinc);
}
AtualizarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
}
conexaoServidor.SqlTransaction.Commit();
conexaoLocal.SqlTransaction.Commit();
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Erro ao iniciar sincronizacao. {0}", exception.Message), exception.InnerException);
}
}
return sincronizacaoSinc;
}
public static bool ValidarConexaoEPI(SqlConnectionEPI conexao) //This method is in another static class
{
try
{
conexao.Open();
}
catch (SqlException sqlException)
{
var dataSource = conexao != null && conexao.SqlConnection != null
? conexao.SqlConnection.DataSource : "string de conexão inválida";
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados ({0}).", dataSource), sqlException);
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados: {0}", exception.Message), exception);
}
return true;
}
But, after rethrow I lost the exception data. The next method, the one who called the SolicitarNovaSincronizacao() receive a NullReference exception. As you can see:
Exception catch on ValidarConexaoEPI():
NullReference exception catch:
Every other exception catch by SolicitarNovaSincronizacao
is ok, the problem occur just with this one. The exception used to mount the new one is ok.
PS.: I know that it is not the right use of try/catch! Not my code.
EDIT:
Here is the StackTrace of the NullReferenceException:
em AG.ControleEPI.Dados.SqlConnectionEPI.Dispose() na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSqlConnectionEPI.cs:linha 28
em AG.ControleEPI.Dados.ADO.SincronizacaoDataAccess.SolicitarNovaSincronizacao(Nullable`1 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao, Boolean validaSincronizacaoEmAndatmento) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSincronizacaoOnlineSincronizacaoDataAccess.cs:linha 50
em AG.ControleEPI.Negocio.Gerenciador.SincronizacaoGerenciador.IniciarSincronizacao(Int32 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.NegocioGerenciadorCustomizadoSincronizacaoGerenciador.cs:linha 84
c# exception
|
show 5 more comments
up vote
1
down vote
favorite
Good morning, guys!
I have a method ValidarConexaoEPI
wich validade a connection string trying to open a connection with it. In case of exceptions, the method who had call the "validate" one SolicitarNovaSincronizacao
, catchs it and rethrow a manipulated exception.
public static SincronizacaoSinc SolicitarNovaSincronizacao(int? idUsuario, int idAlmoxarifado, string versaoAplicacao, bool validaSincronizacaoEmAndatmento = true)
{
SincronizacaoSinc sincronizacaoSinc = null;
using (var conexaoLocal = new SqlConnectionEPI(_DbLocalConnectionString))
using (var conexaoServidor = new SqlConnectionEPI(_DbOnlineConnectionString))
{
sincronizacaoSinc = new SincronizacaoSinc(idUsuario, idAlmoxarifado, versaoAplicacao);
try
{
if (UtilDataAccess.ValidarConexaoEPI(conexaoLocal) && UtilDataAccess.ValidarConexaoEPI(conexaoServidor))
{
ValidarAlmoxarifadoBaseOnline(conexaoServidor, idAlmoxarifado);
ValidarUsuarioBaseOnline(conexaoServidor, idUsuario);
CriarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
if(validaSincronizacaoEmAndatmento)
{
SolicitarSincronizacao(conexaoServidor, sincronizacaoSinc);
}
AtualizarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
}
conexaoServidor.SqlTransaction.Commit();
conexaoLocal.SqlTransaction.Commit();
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Erro ao iniciar sincronizacao. {0}", exception.Message), exception.InnerException);
}
}
return sincronizacaoSinc;
}
public static bool ValidarConexaoEPI(SqlConnectionEPI conexao) //This method is in another static class
{
try
{
conexao.Open();
}
catch (SqlException sqlException)
{
var dataSource = conexao != null && conexao.SqlConnection != null
? conexao.SqlConnection.DataSource : "string de conexão inválida";
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados ({0}).", dataSource), sqlException);
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados: {0}", exception.Message), exception);
}
return true;
}
But, after rethrow I lost the exception data. The next method, the one who called the SolicitarNovaSincronizacao() receive a NullReference exception. As you can see:
Exception catch on ValidarConexaoEPI():
NullReference exception catch:
Every other exception catch by SolicitarNovaSincronizacao
is ok, the problem occur just with this one. The exception used to mount the new one is ok.
PS.: I know that it is not the right use of try/catch! Not my code.
EDIT:
Here is the StackTrace of the NullReferenceException:
em AG.ControleEPI.Dados.SqlConnectionEPI.Dispose() na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSqlConnectionEPI.cs:linha 28
em AG.ControleEPI.Dados.ADO.SincronizacaoDataAccess.SolicitarNovaSincronizacao(Nullable`1 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao, Boolean validaSincronizacaoEmAndatmento) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSincronizacaoOnlineSincronizacaoDataAccess.cs:linha 50
em AG.ControleEPI.Negocio.Gerenciador.SincronizacaoGerenciador.IniciarSincronizacao(Int32 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.NegocioGerenciadorCustomizadoSincronizacaoGerenciador.cs:linha 84
c# exception
5
Side note: putexception
itself, notexception.InnerException
herethrow new Exception(String.Format( "Erro ao iniciar sincronizacao. {0}", exception.Message), exception);
– Dmitry Bychenko
Nov 19 at 13:32
I was initially using it. Just made the change trying to solve my problem. I appreciate!
– Marco Talles Silva
Nov 19 at 13:41
You're still passingexception.InnerException
insideSolicitarNovaSincronizacao
, which could well be null...
– Ian
Nov 19 at 13:46
2
Consider using English naming - it is a widely used practice and make it much easier for everyone: for people on StackOverflow trying to help you, for you as a developer to be able work in international projects or to relocate in future, for consumers of your code, for prospective international employees etc.
– Yeldar Kurmangaliyev
Nov 19 at 13:48
I'm passing just the exception, guys. The exception.InnerException was just a mad try to correct my problem as I said. Already made the change back! Thank you!
– Marco Talles Silva
Nov 19 at 13:51
|
show 5 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Good morning, guys!
I have a method ValidarConexaoEPI
wich validade a connection string trying to open a connection with it. In case of exceptions, the method who had call the "validate" one SolicitarNovaSincronizacao
, catchs it and rethrow a manipulated exception.
public static SincronizacaoSinc SolicitarNovaSincronizacao(int? idUsuario, int idAlmoxarifado, string versaoAplicacao, bool validaSincronizacaoEmAndatmento = true)
{
SincronizacaoSinc sincronizacaoSinc = null;
using (var conexaoLocal = new SqlConnectionEPI(_DbLocalConnectionString))
using (var conexaoServidor = new SqlConnectionEPI(_DbOnlineConnectionString))
{
sincronizacaoSinc = new SincronizacaoSinc(idUsuario, idAlmoxarifado, versaoAplicacao);
try
{
if (UtilDataAccess.ValidarConexaoEPI(conexaoLocal) && UtilDataAccess.ValidarConexaoEPI(conexaoServidor))
{
ValidarAlmoxarifadoBaseOnline(conexaoServidor, idAlmoxarifado);
ValidarUsuarioBaseOnline(conexaoServidor, idUsuario);
CriarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
if(validaSincronizacaoEmAndatmento)
{
SolicitarSincronizacao(conexaoServidor, sincronizacaoSinc);
}
AtualizarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
}
conexaoServidor.SqlTransaction.Commit();
conexaoLocal.SqlTransaction.Commit();
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Erro ao iniciar sincronizacao. {0}", exception.Message), exception.InnerException);
}
}
return sincronizacaoSinc;
}
public static bool ValidarConexaoEPI(SqlConnectionEPI conexao) //This method is in another static class
{
try
{
conexao.Open();
}
catch (SqlException sqlException)
{
var dataSource = conexao != null && conexao.SqlConnection != null
? conexao.SqlConnection.DataSource : "string de conexão inválida";
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados ({0}).", dataSource), sqlException);
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados: {0}", exception.Message), exception);
}
return true;
}
But, after rethrow I lost the exception data. The next method, the one who called the SolicitarNovaSincronizacao() receive a NullReference exception. As you can see:
Exception catch on ValidarConexaoEPI():
NullReference exception catch:
Every other exception catch by SolicitarNovaSincronizacao
is ok, the problem occur just with this one. The exception used to mount the new one is ok.
PS.: I know that it is not the right use of try/catch! Not my code.
EDIT:
Here is the StackTrace of the NullReferenceException:
em AG.ControleEPI.Dados.SqlConnectionEPI.Dispose() na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSqlConnectionEPI.cs:linha 28
em AG.ControleEPI.Dados.ADO.SincronizacaoDataAccess.SolicitarNovaSincronizacao(Nullable`1 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao, Boolean validaSincronizacaoEmAndatmento) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSincronizacaoOnlineSincronizacaoDataAccess.cs:linha 50
em AG.ControleEPI.Negocio.Gerenciador.SincronizacaoGerenciador.IniciarSincronizacao(Int32 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.NegocioGerenciadorCustomizadoSincronizacaoGerenciador.cs:linha 84
c# exception
Good morning, guys!
I have a method ValidarConexaoEPI
wich validade a connection string trying to open a connection with it. In case of exceptions, the method who had call the "validate" one SolicitarNovaSincronizacao
, catchs it and rethrow a manipulated exception.
public static SincronizacaoSinc SolicitarNovaSincronizacao(int? idUsuario, int idAlmoxarifado, string versaoAplicacao, bool validaSincronizacaoEmAndatmento = true)
{
SincronizacaoSinc sincronizacaoSinc = null;
using (var conexaoLocal = new SqlConnectionEPI(_DbLocalConnectionString))
using (var conexaoServidor = new SqlConnectionEPI(_DbOnlineConnectionString))
{
sincronizacaoSinc = new SincronizacaoSinc(idUsuario, idAlmoxarifado, versaoAplicacao);
try
{
if (UtilDataAccess.ValidarConexaoEPI(conexaoLocal) && UtilDataAccess.ValidarConexaoEPI(conexaoServidor))
{
ValidarAlmoxarifadoBaseOnline(conexaoServidor, idAlmoxarifado);
ValidarUsuarioBaseOnline(conexaoServidor, idUsuario);
CriarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
if(validaSincronizacaoEmAndatmento)
{
SolicitarSincronizacao(conexaoServidor, sincronizacaoSinc);
}
AtualizarRegistroSincronizacao(conexaoLocal, conexaoServidor, sincronizacaoSinc);
}
conexaoServidor.SqlTransaction.Commit();
conexaoLocal.SqlTransaction.Commit();
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Erro ao iniciar sincronizacao. {0}", exception.Message), exception.InnerException);
}
}
return sincronizacaoSinc;
}
public static bool ValidarConexaoEPI(SqlConnectionEPI conexao) //This method is in another static class
{
try
{
conexao.Open();
}
catch (SqlException sqlException)
{
var dataSource = conexao != null && conexao.SqlConnection != null
? conexao.SqlConnection.DataSource : "string de conexão inválida";
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados ({0}).", dataSource), sqlException);
}
catch (Exception exception)
{
throw new Exception(String.Format(
"Não foi possível estabelecer uma conexão com o banco de dados: {0}", exception.Message), exception);
}
return true;
}
But, after rethrow I lost the exception data. The next method, the one who called the SolicitarNovaSincronizacao() receive a NullReference exception. As you can see:
Exception catch on ValidarConexaoEPI():
NullReference exception catch:
Every other exception catch by SolicitarNovaSincronizacao
is ok, the problem occur just with this one. The exception used to mount the new one is ok.
PS.: I know that it is not the right use of try/catch! Not my code.
EDIT:
Here is the StackTrace of the NullReferenceException:
em AG.ControleEPI.Dados.SqlConnectionEPI.Dispose() na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSqlConnectionEPI.cs:linha 28
em AG.ControleEPI.Dados.ADO.SincronizacaoDataAccess.SolicitarNovaSincronizacao(Nullable`1 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao, Boolean validaSincronizacaoEmAndatmento) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.DadosADOSincronizacaoOnlineSincronizacaoDataAccess.cs:linha 50
em AG.ControleEPI.Negocio.Gerenciador.SincronizacaoGerenciador.IniciarSincronizacao(Int32 idUsuario, Int32 idAlmoxarifado, String versaoAplicacao) na C:ProjetosAGTFS2013ControleEPIMainSource-locaisAG.ControleEPI.NegocioGerenciadorCustomizadoSincronizacaoGerenciador.cs:linha 84
c# exception
c# exception
edited Nov 19 at 13:57
asked Nov 19 at 13:29
Marco Talles Silva
86
86
5
Side note: putexception
itself, notexception.InnerException
herethrow new Exception(String.Format( "Erro ao iniciar sincronizacao. {0}", exception.Message), exception);
– Dmitry Bychenko
Nov 19 at 13:32
I was initially using it. Just made the change trying to solve my problem. I appreciate!
– Marco Talles Silva
Nov 19 at 13:41
You're still passingexception.InnerException
insideSolicitarNovaSincronizacao
, which could well be null...
– Ian
Nov 19 at 13:46
2
Consider using English naming - it is a widely used practice and make it much easier for everyone: for people on StackOverflow trying to help you, for you as a developer to be able work in international projects or to relocate in future, for consumers of your code, for prospective international employees etc.
– Yeldar Kurmangaliyev
Nov 19 at 13:48
I'm passing just the exception, guys. The exception.InnerException was just a mad try to correct my problem as I said. Already made the change back! Thank you!
– Marco Talles Silva
Nov 19 at 13:51
|
show 5 more comments
5
Side note: putexception
itself, notexception.InnerException
herethrow new Exception(String.Format( "Erro ao iniciar sincronizacao. {0}", exception.Message), exception);
– Dmitry Bychenko
Nov 19 at 13:32
I was initially using it. Just made the change trying to solve my problem. I appreciate!
– Marco Talles Silva
Nov 19 at 13:41
You're still passingexception.InnerException
insideSolicitarNovaSincronizacao
, which could well be null...
– Ian
Nov 19 at 13:46
2
Consider using English naming - it is a widely used practice and make it much easier for everyone: for people on StackOverflow trying to help you, for you as a developer to be able work in international projects or to relocate in future, for consumers of your code, for prospective international employees etc.
– Yeldar Kurmangaliyev
Nov 19 at 13:48
I'm passing just the exception, guys. The exception.InnerException was just a mad try to correct my problem as I said. Already made the change back! Thank you!
– Marco Talles Silva
Nov 19 at 13:51
5
5
Side note: put
exception
itself, not exception.InnerException
here throw new Exception(String.Format( "Erro ao iniciar sincronizacao. {0}", exception.Message), exception);
– Dmitry Bychenko
Nov 19 at 13:32
Side note: put
exception
itself, not exception.InnerException
here throw new Exception(String.Format( "Erro ao iniciar sincronizacao. {0}", exception.Message), exception);
– Dmitry Bychenko
Nov 19 at 13:32
I was initially using it. Just made the change trying to solve my problem. I appreciate!
– Marco Talles Silva
Nov 19 at 13:41
I was initially using it. Just made the change trying to solve my problem. I appreciate!
– Marco Talles Silva
Nov 19 at 13:41
You're still passing
exception.InnerException
inside SolicitarNovaSincronizacao
, which could well be null...– Ian
Nov 19 at 13:46
You're still passing
exception.InnerException
inside SolicitarNovaSincronizacao
, which could well be null...– Ian
Nov 19 at 13:46
2
2
Consider using English naming - it is a widely used practice and make it much easier for everyone: for people on StackOverflow trying to help you, for you as a developer to be able work in international projects or to relocate in future, for consumers of your code, for prospective international employees etc.
– Yeldar Kurmangaliyev
Nov 19 at 13:48
Consider using English naming - it is a widely used practice and make it much easier for everyone: for people on StackOverflow trying to help you, for you as a developer to be able work in international projects or to relocate in future, for consumers of your code, for prospective international employees etc.
– Yeldar Kurmangaliyev
Nov 19 at 13:48
I'm passing just the exception, guys. The exception.InnerException was just a mad try to correct my problem as I said. Already made the change back! Thank you!
– Marco Talles Silva
Nov 19 at 13:51
I'm passing just the exception, guys. The exception.InnerException was just a mad try to correct my problem as I said. Already made the change back! Thank you!
– Marco Talles Silva
Nov 19 at 13:51
|
show 5 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375692%2fexception-lost-after-rethrow%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Side note: put
exception
itself, notexception.InnerException
herethrow new Exception(String.Format( "Erro ao iniciar sincronizacao. {0}", exception.Message), exception);
– Dmitry Bychenko
Nov 19 at 13:32
I was initially using it. Just made the change trying to solve my problem. I appreciate!
– Marco Talles Silva
Nov 19 at 13:41
You're still passing
exception.InnerException
insideSolicitarNovaSincronizacao
, which could well be null...– Ian
Nov 19 at 13:46
2
Consider using English naming - it is a widely used practice and make it much easier for everyone: for people on StackOverflow trying to help you, for you as a developer to be able work in international projects or to relocate in future, for consumers of your code, for prospective international employees etc.
– Yeldar Kurmangaliyev
Nov 19 at 13:48
I'm passing just the exception, guys. The exception.InnerException was just a mad try to correct my problem as I said. Already made the change back! Thank you!
– Marco Talles Silva
Nov 19 at 13:51