user9005650
user9005650

Reputation:

my class can't use try catch block when returning values

How to use try catch block within a class that you created, i have used methods that return values. so when i try to use try catch block it says an Error it says not all code paths return a value.

public int Save_Del_Up(string query)
{
   try
   {
     con.Open();
     cmd = new SqlCommand(query, con);
     int result = cmd.ExecuteNonQuery();
     con.Close();

     return result;
   }
   catch ()
   {
   }
}

Upvotes: 0

Views: 56

Answers (1)

ztadic91
ztadic91

Reputation: 2804

as the error says you need to return something from the catch block

public int Save_Del_Up(string query)
     try
     {    
       con.Open();
       cmd = new SqlCommand(query, con);
       int result = cmd.ExecuteNonQuery();

       return result;
    } 
    catch (Exception ex) 
    {
        return -1;
    }
    finally
    {
     con.Close();
    }
}

Upvotes: 1

Related Questions