Try catch finally
-
Hi, Maby this is simple for you, but for me is not. I have this code:
int rezultat = 0;
try
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
rezultat = (int)cmd.ExecuteScalar();} catch (Exception ex) { lblMesaje.Text = "Eroare: " + ex.Message.ToString(); } finally { if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); } } return rezultat;
Is just for inserting a new record in a table. Even if this throw an error "Specified cast is not valid." "rezultat=(int)cmd.ExecuteScalar();" - the code is executed and the row is inserted in the database, and the execution continues. Why it continues? Maby i don't understand the try catch finally yet :) Thank you!
-
Hi, Maby this is simple for you, but for me is not. I have this code:
int rezultat = 0;
try
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
rezultat = (int)cmd.ExecuteScalar();} catch (Exception ex) { lblMesaje.Text = "Eroare: " + ex.Message.ToString(); } finally { if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); } } return rezultat;
Is just for inserting a new record in a table. Even if this throw an error "Specified cast is not valid." "rezultat=(int)cmd.ExecuteScalar();" - the code is executed and the row is inserted in the database, and the execution continues. Why it continues? Maby i don't understand the try catch finally yet :) Thank you!
The whole idea of try...catch blocks is to do exactly what you have done: catch an error, report it (or log it, or fix it) and let the software continue without crashing. In your case, the exception is being thrown because the result returned by SQL is not an integer, and cannot be cast to an integer (it may be a DBNull, or a string for example - either put a breakpoint on the ExecuteScalar line and look at the return, or include the return value in your message to actually fix the problem). But the insert has been done by that time, so the row is in the DB already.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
-
The whole idea of try...catch blocks is to do exactly what you have done: catch an error, report it (or log it, or fix it) and let the software continue without crashing. In your case, the exception is being thrown because the result returned by SQL is not an integer, and cannot be cast to an integer (it may be a DBNull, or a string for example - either put a breakpoint on the ExecuteScalar line and look at the return, or include the return value in your message to actually fix the problem). But the insert has been done by that time, so the row is in the DB already.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
Thank you for your message! Beceause now i try to learn some C# programming (and write in english), your message help me to understand about Try catch. Just to be sure that i have understand: 1. I have my btnSave code that look like this:
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
int x = 0; //this will be the ID of the new inserted record
x = SaveData();
if (x > 0)
{
lblMesaje.Text = " Your new ID is: " + x.ToString();
}
}
catch (Exception ex)
{
lblMesage.Text = "Some error occured: " + ex.Message;
}}
2. Now here is my Insert function:
private int SaveData()
{
//SQL objects set..int result= 0; try { if (sqlconn.State != ConnectionState.Open) { sqlconn.Open(); } result= Convert.ToInt32(cmd.ExecuteScalar()); return result; } catch (Exception) { throw; } finally { if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); } } }
Now i throw and exception in SaveData function and the message is displayed to the user. Is ok? Thanks again!
-
Thank you for your message! Beceause now i try to learn some C# programming (and write in english), your message help me to understand about Try catch. Just to be sure that i have understand: 1. I have my btnSave code that look like this:
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
int x = 0; //this will be the ID of the new inserted record
x = SaveData();
if (x > 0)
{
lblMesaje.Text = " Your new ID is: " + x.ToString();
}
}
catch (Exception ex)
{
lblMesage.Text = "Some error occured: " + ex.Message;
}}
2. Now here is my Insert function:
private int SaveData()
{
//SQL objects set..int result= 0; try { if (sqlconn.State != ConnectionState.Open) { sqlconn.Open(); } result= Convert.ToInt32(cmd.ExecuteScalar()); return result; } catch (Exception) { throw; } finally { if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); } } }
Now i throw and exception in SaveData function and the message is displayed to the user. Is ok? Thanks again!
:thumbsup: Looks good!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
-
Thank you for your message! Beceause now i try to learn some C# programming (and write in english), your message help me to understand about Try catch. Just to be sure that i have understand: 1. I have my btnSave code that look like this:
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
int x = 0; //this will be the ID of the new inserted record
x = SaveData();
if (x > 0)
{
lblMesaje.Text = " Your new ID is: " + x.ToString();
}
}
catch (Exception ex)
{
lblMesage.Text = "Some error occured: " + ex.Message;
}}
2. Now here is my Insert function:
private int SaveData()
{
//SQL objects set..int result= 0; try { if (sqlconn.State != ConnectionState.Open) { sqlconn.Open(); } result= Convert.ToInt32(cmd.ExecuteScalar()); return result; } catch (Exception) { throw; } finally { if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); } } }
Now i throw and exception in SaveData function and the message is displayed to the user. Is ok? Thanks again!
This is fine, although the
Claudiu Schiopu wrote:
catch (Exception) { throw; }
doesn't really do much. You can just have the
try
and
finally
blocks, and the code would effectively do the same thing.
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen