try-catch question

D

Dom

I'vw decided to use a try-catch setup, because of a recurring bug. I
started off with just catch (SqlException), and that worked, so I got
fancier and added a catch (Exception) after it. Now the last catch is
always caught.

Here is a good snippet of the code. The "m_xxx" variables are all
class-level, and I think you can figure out the types.



public SqlDataReader test()
m_Cnn = new SqlConnection(m_ConnectionString);
m_Cnn.Open();
m_Cmd = new SqlCommand(c, m_Cnn);
try
{
m_Rdr = m_Cmd.ExecuteReader();
}
catch (SqlException e)
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader();
}
catch (Exception e)
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader();
}
return m_Rdr;
 
A

Arne Vajhøj

Dom said:
I'vw decided to use a try-catch setup, because of a recurring bug. I
started off with just catch (SqlException), and that worked, so I got
fancier and added a catch (Exception) after it. Now the last catch is
always caught.

Here is a good snippet of the code. The "m_xxx" variables are all
class-level, and I think you can figure out the types.

public SqlDataReader test()
m_Cnn = new SqlConnection(m_ConnectionString);
m_Cnn.Open();
m_Cmd = new SqlCommand(c, m_Cnn);
try
{
m_Rdr = m_Cmd.ExecuteReader();
}
catch (SqlException e)
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader();
}
catch (Exception e)
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader();
}
return m_Rdr;

1 { and 3 } ?

I don't believe this compiles !

Arne
 
P

Peter Bromberg [C# MVP]

At a minimum, for the code to even compile successfully, it would need to
look like this:

public SqlDataReader test()
{
m_Cnn = new SqlConnection(m_ConnectionString);
m_Cnn.Open();
m_Cmd = new SqlCommand(c, m_Cnn);
try
{
m_Rdr = m_Cmd.ExecuteReader();
}
catch (SqlException e)
{
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader(); //don't do business logic inside catch!
}
catch (Exception e)
{
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader(); //don't do business logic inside catch!
}
return m_Rdr;
}

-- If you are going to catch exceptions, you need a strategy of what to do
with them.
--Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
 
D

Dom

1 { and 3 } ?

I don't believe this compiles !

Arne- Hide quoted text -

- Show quoted text -

Let me try it again:


public SqlDataReader test()
{
m_Cnn = new SqlConnection(m_ConnectionString);
m_Cnn.Open();
m_Cmd = new SqlCommand(c, m_Cnn);
try
{
m_Rdr = m_Cmd.ExecuteReader();


}


catch (SqlException e)
{
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader();

}


catch (Exception e)
{
m_Cmd.CommandText = "<...>"
m_Rdr = m_Cmd.ExecuteReader();

}
}
 
D

Dom

At a minimum, for the code to even compile successfully, it would need to
look like this:

public SqlDataReader test()
{
m_Cnn = new SqlConnection(m_ConnectionString);
m_Cnn.Open();
m_Cmd = new SqlCommand(c, m_Cnn);
try
{
    m_Rdr = m_Cmd.ExecuteReader();}

catch (SqlException e)
 {
     m_Cmd.CommandText = "<...>"
     m_Rdr = m_Cmd.ExecuteReader();  //don't do business logic inside catch!
 }
catch (Exception e)
{
     m_Cmd.CommandText = "<...>"
     m_Rdr = m_Cmd.ExecuteReader(); //don't do business logic inside catch!

}
return m_Rdr;
}

-- If you are going to catch exceptions, you need a strategy of what to do
with them.
--Peter
Site:http://www.eggheadcafe.com
UnBlog:http://petesbloggerama.blogspot.com
Short Urls & more:http://ittyurl.net







- Show quoted text -

That's not answering my question. Why is the catch (Exception)
getting called so often. When I remove I do not get a unhandled
exception.

As far as the business logic, the method returns a SqlDataReader. In
the case of a catch, I want SqlDataReader will still return, but it is
empty, ie, it has no records. This seemed like an easy way out of my
problem. But I take your point.

Dom
 
S

scottmyers

That's not answering my question.  Why is the catch (Exception)
getting called so often.  When I remove I do not get a unhandled
exception.

As far as the business logic, the method returns a SqlDataReader.  In
the case of a catch, I want SqlDataReader will still return, but it is
empty, ie, it has no records.  This seemed like an easy way out of my
problem.  But I take your point.

Dom- Hide quoted text -

- Show quoted text -

Is the command that you are trying to execute the same in all 3
cases(the try, the catch SqlException, the catch Exception blocks)?
The whole idea of catching an exception is not so that you can try and
do exactly the same thing again, give us an explanation of what you
are trying to do and include the command text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top