try-catch-finally question 
Author Message
 try-catch-finally question

I've got the following code block in which I try to connect to the database.
Now I would like to issue a SQL query against this database if it opens,
would I (or should I) also do another try-catch-finally block within the
first try to issue the query, or should I create a new public method to do
this.  What is the best preferred approach to OOP and C# with database.
Smaller code sections is better?  Thanks - Zack

try
{
  Console.WriteLine("Opening Connection")
  dbConnection.Open();

Quote:
}

catch(System.Data.OleDB.OleDbException e)
{
  Console.Writeln("Error {0}", e);
Quote:
}

finally
{
  Console.WriteLine("Closing Connection");
  dbConnection.Close();
Quote:
}



Mon, 17 Jan 2005 00:20:21 GMT  
 try-catch-finally question
Personally, I would just put the sql issuing statement in the same try
block. Then catch for the different kinds of exceptions that can be thrown.
There is such a thing as going overboard, you don't want every line of code
to be wrapped in a try/catch block.


Quote:
> I've got the following code block in which I try to connect to the
database.
> Now I would like to issue a SQL query against this database if it opens,
> would I (or should I) also do another try-catch-finally block within the
> first try to issue the query, or should I create a new public method to do
> this.  What is the best preferred approach to OOP and C# with database.
> Smaller code sections is better?  Thanks - Zack

> try
> {
>   Console.WriteLine("Opening Connection")
>   dbConnection.Open();
> }
> catch(System.Data.OleDB.OleDbException e)
> {
>   Console.Writeln("Error {0}", e);
> }
> finally
> {
>   Console.WriteLine("Closing Connection");
>   dbConnection.Close();
> }



Mon, 17 Jan 2005 00:49:50 GMT  
 try-catch-finally question



Quote:
> Personally, I would just put the sql issuing statement in the same try
> block. Then catch for the different kinds of exceptions that can be
thrown.
> There is such a thing as going overboard, you don't want every line of
code
> to be wrapped in a try/catch block.

While agreeing with Marina, there is still a potential
NullReferenceException in your code:

Quote:
> > try
> > {
> >   Console.WriteLine("Opening Connection")
> >   dbConnection.Open();
> > }
> > catch(System.Data.OleDB.OleDbException e)
> > {
> >   Console.Writeln("Error {0}", e);
> > }
> > finally
> > {
> >   Console.WriteLine("Closing Connection");
> >   dbConnection.Close();
> > }

Make that:
if (dbConnection != null)
{
    Console.WriteLine("Closing Connection");
    dbConnection.Close();

Quote:
}

That's an programming idiom you'll find practically everywhere. It's
cross-platform, cross-language and thus bigger and better than J2EE and .NET
combined ;->

Cheers,
--
Joerg Jooss



Mon, 17 Jan 2005 06:34:28 GMT  
 try-catch-finally question
Thanks Marina and Joerg,  working pretty good so far for all my test errors.

Zack


Quote:



> > Personally, I would just put the sql issuing statement in the same try
> > block. Then catch for the different kinds of exceptions that can be
> thrown.
> > There is such a thing as going overboard, you don't want every line of
> code
> > to be wrapped in a try/catch block.

> While agreeing with Marina, there is still a potential
> NullReferenceException in your code:

> > > try
> > > {
> > >   Console.WriteLine("Opening Connection")
> > >   dbConnection.Open();
> > > }
> > > catch(System.Data.OleDB.OleDbException e)
> > > {
> > >   Console.Writeln("Error {0}", e);
> > > }
> > > finally
> > > {
> > >   Console.WriteLine("Closing Connection");
> > >   dbConnection.Close();
> > > }

> Make that:
> if (dbConnection != null)
> {
>     Console.WriteLine("Closing Connection");
>     dbConnection.Close();
> }

> That's an programming idiom you'll find practically everywhere. It's
> cross-platform, cross-language and thus bigger and better than J2EE and
.NET
> combined ;->

> Cheers,
> --
> Joerg Jooss




Mon, 17 Jan 2005 20:38:32 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. try-catch-finally problem (I think?)

2. Problem in Debug mode in try-catch- finally

3. What is finally in try/catch for?

4. Does Finally execute try Catch throws an Exception?

5. How to: try/catch/finally with FileStream ?

6. Bug in .net with Try / Catch / Finally

7. Help on C++ try/catch/finally

8. try..catch..finally

9. Nesting try, catch, finally

10. Try-catch-finally

11. TRY-CATCH vs. try-catch

12. try/catch VS TRY/CATCH

 

 
Powered by phpBB® Forum Software