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










share|improve this question




















  • 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










  • 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






  • 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















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










share|improve this question




















  • 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










  • 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






  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 13:57

























asked Nov 19 at 13:29









Marco Talles Silva

86




86








  • 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










  • 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






  • 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




    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










  • You're still passing exception.InnerException inside SolicitarNovaSincronizacao, 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin