Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have an API in which I have implemented custom exception filter. so, basically I don't have try/catch block in my controller. Just customErrrorAtrribute is there in my controller.

Normally, I write code for my database connectivity is like below.

C#
try
{
Sqlconnection con = new  Sqlconnection("connection string"); 
con.open(); 
sqlcommand cmd  = new sqlcommand("query",con);
cmd.executenonquery();
}
catch(exception ex)
{
// log exception here. 
}
finally
{
con.close(); 
}


now, because i don't have try/catch/finally in my main method, where should i close the connection if I get any error while executing a query.

Note: sample code is just for example purpose, I would have seperate layer to call database related things instead of directly opening and closing connection from the API controller.

What I have tried:

I have tried to check if we can have finally block somewhere in the custom exception filter class but somehow i couldn't achieve it.
Posted
Updated 25-May-22 3:59am

You have moved the small peace of code reporting any exception as an API Json object (or whatever you use) - anything else related to try/catch/finally blocks should still remaining where it is.

So you still need try/finally blocks to ensure items are disposed (or closed, but disposed is the more generic term). Try googling "C# using statement". That is how we normally write the call to Close (which is typically called from Dispose) without having to write the trivial finalizer code each time.

While this seems trivial and "there must be a better way" the answer to that is quite simple: there isn't. :)
 
Share this answer
 
Comments
Ravi-from-India 25-May-22 9:49am    
Thanks mate.
Always wrap disposable objects such as SqlConnection / SqlCommand in using blocks. That way, they will always be disposed of properly.

Eg:
C#
using (SqlConnection con = new SqlConnection("connection string"))
using (SqlCommand cmd  = new SqlCommand("query", con))
{
    con.Open(); 
    cmd.ExecuteNonQuery();
}
is equivalent to:
C#
{
    SqlConnection con = new SqlConnection("connection string");
    try
    {
        SqlCommand cmd = new SqlCommand("query", con);
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        finally
        {
            if (cmd != null) cmd.Dispose();
        }
    }
    finally
    {
        if (con != null) con.Dispose();
    }
}

If you're using C# 8.0 or later, you don't even need the curly braces - a using declaration applies to the end of the enclosing block:
C#
using SqlConnection con = new SqlConnection("connection string");
using SqlCommand cmd = new SqlCommand("query", con);
con.Open();
cmd.ExecuteNonQuery();

using statement - C# Reference | Microsoft Docs[^]
 
Share this answer
 
Comments
Ravi-from-India 26-May-22 2:27am    
Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900