Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello to this method I want to do a try catch blog.
Is it true that I use try catch in this way? if the condition is true try is not an error.

public void Insert()
    {
        DataTable table= Dbc.GetDataTable( "select //sql command continue where ıd>3 ,commandtype.text );

    if(table.rows.count>0)
    {
    List<int> List=(from row in table select //sql command continue)).ToList();

    if(List.Count() != 2)
    {
        Complete(table);
    }

    DeleteFalse();
    }
    }


What I have tried:

I try code

public void Insert()
            {
    try{
                DataTable table= Dbc.GetDataTable( "select //sql command continue where ıd>3 ,commandtype.text );

            if(table.rows.count>0)
            {
            List<int> List=(from row in table select //sql command continue)).ToList();

            if(List.Count() != 2)
            {
                Complete(table);
            }

            DeleteFalse();
            }
    }
    catch (Exception exceptions) {
          Console.WriteLine("Error");
        }
            }
Posted
Updated 5-Jan-19 12:07pm
v2
Comments
Eric Lynch 3-Jan-19 15:17pm    
Typically, if you have a catch block, you should DO something with the exception. There are many possible actions you can take. Some common options include logging the text of the exception, re-trying the operation (for more specific exceptions like timeout), and returning an indication that the operation failed. It is seldom a good idea to simply discard the exception (as your "have tried" code would do).

It might help if you improve your question to describe your problem a little bit better. What were your expectations from the code you tried? How did the results you observed fail to meet those expectations? Its unclear from your current question if you are simply struggling to understand try/catch or you have some specific, desired outcome that you are having trouble implementing.
[no name] 3-Jan-19 15:32pm    
Hello Adding method is doing my process. But I want this method returns a false result I want to return to the catch. So this methodum is already running. If it does not work, I want to fall into the catch. Thank you for your help.
Eric Lynch 3-Jan-19 16:56pm    
Sorry, I am a bit confused about your goals. I think you may be misunderstanding a few different concepts.

In your reply, you indicate an expectation for your method to return "a false result". Currently, your method cannot return any result, it is of type "void". If you want it to return a true/false result, you need to change its datatype to "bool". Though, based on the remainder of your sentence and your code, it seems unlikely that this is actually your intent.

Next, you indicate that you want to "return to the catch". Perhaps, you're using imprecise terminology, but this is not exactly how a "catch" works.

In the "have tried" section of your question, if any error (exception) were to occur in the "try" block, you would stop executing the code in that "try" block.

Instead, at that point, you would begin executing the first line of code contained in the "catch" block. Since, you currently have no code in that "catch" block, you would do nothing. Effectively, you would discard (ignore) the exception. You would then continue to the next line of code after the "catch" block.

Since you have no code after the "catch" block, you would then return nothing (void) from the "Adding" method.

While I am fairly certain this is not what you intend, I am less clear on what you do intend.

Rather than replying to my comment, I again you suggest you click "Improve Question" and edit your question to add more detail.

You need to be specific about at least two things:
1) What EXACTLY do you expect/want to happen if the code in the "try" block succeeds?
2) What EXACTLY do you expect/want to happen if the code in the "try" block fails? This is the code you want to put in the "catch" block.

Currently, in failure, you do nothing. Also, in either success or failure, you return nothing (void) from the "Adding" method.

Keep in mind that your code may fail at almost any line within the "try" block. For example, it might fail during your SQL query, or during your call to FonkA, or during your call to DeleteNonCorrectAdd. Any of these failures, would result in the code "skipping" to the first line of code in your "catch" block.

 
Share this answer
 
Please, read the Eric Lynch[^] comments first.
Then look at these articles, which may help you to understand when to use try - catch block:
How to: Use the Try-Catch Block to Catch Exceptions | Microsoft Docs[^]
try-catch - C# Reference | Microsoft Docs[^]
 
Share this answer
 

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