Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

try-catch question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dom - 04 Apr 2008 22:08 GMT
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;
Arne Vajhøj - 05 Apr 2008 00:05 GMT
> 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
[quoted text clipped - 21 lines]
> }
> return m_Rdr;

1 { and 3 } ?

I don't believe this compiles !

Arne
Dom - 05 Apr 2008 03:20 GMT
> > 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
[quoted text clipped - 29 lines]
>
> - 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();

}
}
Peter Bromberg [C# MVP] - 05 Apr 2008 03:18 GMT
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

> 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
[quoted text clipped - 21 lines]
> }
> return m_Rdr;
Dom - 05 Apr 2008 03:25 GMT
On Apr 4, 10:18 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.NoSpamMaam.com> wrote:
> At a minimum, for the code to even compile successfully, it would need to
> look like this:
[quoted text clipped - 56 lines]
>
> - 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
Peter Duniho - 05 Apr 2008 03:31 GMT
> [...]
> That's not answering my question.

Sometimes the first step is to help the person asking the question to  
rephrase their question so that it makes sense.  Both Arne and Peter B.  
have tried to do that.

Neither Peter's nor your attempt to fix the original code is correct, as  
there are still missing semi-colons.  But more importantly, you haven't  
provide a complete code example, nor have you precisely described what's  
going on.  Without at least one or the other, it's impossible to answer  
the question.

> Why is the catch (Exception)
> getting called so often.  When I remove I do not get a unhandled
> exception.

What exception is being thrown?  In what context is that method being  
called?

If you're catching an exception, it almost certainly was happening before  
and you just didn't notice it.  But for anything more specific than that,  
we need a more specific question.

Pete
scottmyers - 07 Apr 2008 06:54 GMT
> On Apr 4, 10:18 pm, Peter Bromberg [C# MVP]
>
[quoted text clipped - 72 lines]
>
> - 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.