locked
Problem using 'If-else' block inside 'catch' block. RRS feed

  • Question

  • Hello all,

    I have some problem using the 'if-else' statements inside the 'catch' block.

    When I am executing the below code, everytime the label2 is showing the condition inside the 'if()', i.e.,

    एक आम है

    It is not taking the else if()'s condition even if the label1 is 'It is an apple' or else..

    catch(Exception^ ex){
    
    array<String^>^ err1;
    err1 = ex->ToString();
    
    label1->Text = err1[1];
    
    if (err1[1] = L"There is a mango")
    {
         label2->Text = L"एक आम है";
    }
    else if (err1[1] = L" It is an apple")
    {
         label2->Text = L"यह एक सेब है";
    }
    else if 
    {
    }
    .
    .
    .
    .
    else if()
    {
    }
    
    else 
    {
         label2->Text = L"कुछ भी नहीं है";
    }
    
    }

    How to execute all statements depending on the err1?

    Thanks,

    Humaira.

    Tuesday, December 1, 2015 12:07 PM

Answers

  • Sorry for not mentioning it in the above code.

    If you do not show your actual code (or show incomplete code), it is very difficult for us to help you.

    When I look at the code in the previous post

    if (err1[1] == L"There is a mango")
    {
         label2->Text = L"एक आम है";
    }
    else if (err1[1] == L" It is an apple")
    {
         label2->Text = L"यह एक सेब है";
    }

    I see that the first phrase has no space at the beginning, while the second does. Perhaps this is the reason you are not getting a match.

    It might be a good idea to strip leading and trailing white space from err1[1], and to make sure that the test phrases contain no leading or trailing white space.


    David Wilkinson | Visual C++ MVP


    • Marked as answer by Humaira Syed Thursday, December 3, 2015 4:54 AM
    • Edited by davewilk Thursday, December 3, 2015 10:17 AM typos
    Wednesday, December 2, 2015 3:23 PM

All replies

  • Check with == instead of = in the if and else if conditions instead.
    Tuesday, December 1, 2015 12:14 PM
  • Check with == instead of = in the if and else if conditions instead.
    And why are you using an array of String? The ToString() function returns a String, not an array of String.

    David Wilkinson | Visual C++ MVP

    • Proposed as answer by «_Superman_» Wednesday, December 2, 2015 3:16 AM
    Tuesday, December 1, 2015 5:07 PM
  • It is still not working. It is not even showing the 1st 'if() condition' as it was displaying earlier. 

    Only, the else() condition is being displayed every time. 

    I am having a doubt, Can we use if-else's inside catch block?

    If yes, then for executing my code, do I need to change the logic? 

    Thanks,

    Humaira

    Wednesday, December 2, 2015 7:26 AM
  • It is still not working. It is not even showing the 1st 'if() condition' as it was displaying earlier. 

    Only, the else() condition is being displayed every time. 

    I am having a doubt, Can we use if-else's inside catch block?

    If yes, then for executing my code, do I need to change the logic? 

    Of course you can use if .. else inside a catch block. Show your new code.

    Did you make the changes suggested by Jy_13 and myself?


    David Wilkinson | Visual C++ MVP

    Wednesday, December 2, 2015 10:24 AM
  • Yes sir, I have changed as you and Jy_13 suggested.

    Here is the code:

    catch(Exception^ ex){
    
    array<String^>^ err1;
    err1 = ex->ToString();
    
    label1->Text = err1[1];
    
    if (err1[1] == L"There is a mango")
    {
         label2->Text = L"एक आम है";
    }
    else if (err1[1] == L" It is an apple")
    {
         label2->Text = L"यह एक सेब है";
    }
    else if 
    {
    }
    .
    .
    .
    .
    else if()
    {
    }
    
    else 
    {
         label2->Text = L"कुछ भी नहीं है";
    }
    
    }

    The 'label2' must show the Hindi text and 'label1' must show English text. 

    Now, when I am debugging, the 'label2' is always showing 'कुछ भी नहीं है'. It is not changing even when label1 is changing. 

    Thanks,

    Humaira.

    Wednesday, December 2, 2015 11:19 AM
  • Yes sir, I have changed as you and Jy_13 suggested.

    Here is the code:

    catch(Exception^ ex){
    
    array<String^>^ err1;
    err1 = ex->ToString();
    
    label1->Text = err1[1];
    
    if (err1[1] == L"There is a mango")
    {
         label2->Text = L"एक आम है";
    }
    else if (err1[1] == L" It is an apple")
    {
         label2->Text = L"यह एक सेब है";
    }
    else if 
    {
    }
    .
    .
    .
    .
    else if()
    {
    }
    
    else 
    {
         label2->Text = L"कुछ भी नहीं है";
    }
    
    }

    The 'label2' must show the Hindi text and 'label1' must show English text. 

    Now, when I am debugging, the 'label2' is always showing 'कुछ भी नहीं है'. It is not changing even when label1 is changing. 

    Again, why are you using array of String when you should be using String?

    David Wilkinson | Visual C++ MVP

    Wednesday, December 2, 2015 12:00 PM
  • Sorry for not mentioning it in the above code.

    Actually, I have checked by storing the 'ex' in a variable and displayed in a label. There it included a block of message including many lines and some code like 0x0C******, so I have split that line using the below code:

    String^ errmsg = ex->ToString();
    String^ firstline = "\n";
    array<Char>^ limit = firstline->ToCharArray( );
    array<String^>^ err1;
    err1 = errmsg->Split(limit);

    Then the required sentence, eg:'There is a mango' is been split and was stored in the array err1[1]. The err1[0] contains that hexadecimal number.

    After this snippet, I added the if-else block.

    When, I write the code with only one 'if-else'. It is working smoothly. But as I include 'else if()' in between, it is only showing else() condition always. I want to add more else if()'s in the code.

    Thanks,

    Humaira


     
    Wednesday, December 2, 2015 2:26 PM
  • Sorry for not mentioning it in the above code.

    If you do not show your actual code (or show incomplete code), it is very difficult for us to help you.

    When I look at the code in the previous post

    if (err1[1] == L"There is a mango")
    {
         label2->Text = L"एक आम है";
    }
    else if (err1[1] == L" It is an apple")
    {
         label2->Text = L"यह एक सेब है";
    }

    I see that the first phrase has no space at the beginning, while the second does. Perhaps this is the reason you are not getting a match.

    It might be a good idea to strip leading and trailing white space from err1[1], and to make sure that the test phrases contain no leading or trailing white space.


    David Wilkinson | Visual C++ MVP


    • Marked as answer by Humaira Syed Thursday, December 3, 2015 4:54 AM
    • Edited by davewilk Thursday, December 3, 2015 10:17 AM typos
    Wednesday, December 2, 2015 3:23 PM
  • Thank you sir. Now it is working properly. 

    Thanks and Regards,

    Humaira.


    Thursday, December 3, 2015 4:55 AM