When to TRY and not to TRY

G

Guest

Hi all,

I haven't really used the TRY statement much, so am not sure when to use and
and how to use it just yet. Any general hints?

For example, when using it, should a create database statement fall within
the try statement or outside of it, when using the EL (Enterprise Library).

In examples online, not using EL, the create db stuff occurs before the try
statement.

This is how i have implemented it so far!

try
{

//Should this be here?????
Database Db = DatabaseFactory.CreateDatabase();

//create db command object for 'Login_ChangePassword' proc
DbCommand DbCommand =
Db.GetStoredProcCommand("Login_ChangePassword");

//add required parameters
Db.AddInParameter(DbCommand, "@Password", DbType.String,
newPassword);
Db.AddInParameter(DbCommand, "@Username", DbType.String,
username);

//execute command; populate local variable indicating rows
affected
RowsAffected = Db.ExecuteNonQuery(DbCommand);

}
catch (SqlException ex)
{

bool rethrow = ExceptionPolicy.HandleException(ex, "Log Only
Policy");
if (rethrow) throw;

}


Help Appreciated!

Cheers,
 
C

Chris Auer

Hi all,

I haven't really used the TRY statement much, so am not sure when to use and
and how to use it just yet. Any general hints?

For example, when using it, should a create database statement fall within
the try statement or outside of it, when using the EL (Enterprise Library).

In examples online, not using EL, the create db stuff occurs before the try
statement.

This is how i have implemented it so far!

try
{

//Should this be here?????
Database Db = DatabaseFactory.CreateDatabase();

//create db command object for 'Login_ChangePassword' proc
DbCommand DbCommand =
Db.GetStoredProcCommand("Login_ChangePassword");

//add required parameters
Db.AddInParameter(DbCommand, "@Password", DbType.String,
newPassword);
Db.AddInParameter(DbCommand, "@Username", DbType.String,
username);

//execute command; populate local variable indicating rows
affected
RowsAffected = Db.ExecuteNonQuery(DbCommand);

}
catch (SqlException ex)
{

bool rethrow = ExceptionPolicy.HandleException(ex, "Log Only
Policy");
if (rethrow) throw;

}

Help Appreciated!

Cheers,

I would only use the try/catch around things that do work.



try
{
RowsAffected = Db.ExecuteNonQuery(DbCommand);

}
catch (SqlException ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex,
"Log Only Policy");
if (rethrow) throw; .
}
 
S

sloan

There are also try/finally blocks

See this code.


public string WriteToTempFile(string msg)
{
System.IO.StreamWriter writer = null; // the scope needs to be
outside the try block block , so you can "see" it in the finally block
System.IO.FileStream fs = null; //ditto comment from previous
line

try
{

// Writes text to a temporary file and returns path
string fileName = System.IO.Path.GetTempFileName();
fileName = fileName.Replace(".tmp", ".txt");
fs = new System.IO.FileStream(fileName,
System.IO.FileMode.Append, System.IO.FileAccess.Write);
// Opens stream and begins writing
writer = new System.IO.StreamWriter(fs);
writer.BaseStream.Seek(0, System.IO.SeekOrigin.End);
writer.WriteLine(msg);
writer.Flush();

return fileName;

}
finally
{
//even if an exception occurs, the finally runs. it forces
a clean up of objects

if (null != writer) //check for null to make sure it didn't
error before it was created
{
writer.Close();
}
if (null != fs)
{
fs.Close();
}
}
}




A possible shorthand of this is the 'using' statement.

sometimes you'll see this

using ( myConcreteObject )
{

}


or even

using (myOtherConcreteObject as IDisposable)
{

}
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top