locked
How to catch exceptions RRS feed

  • Question

  • I thought I had a general understanding of try-catch blocks but apparently not.

     

     
    DataTable dt = null;
    	      try 
    	      {
    	      	dt = oDebug.funcImportExcelSheetIntoDataTable(sheet, "SELECT * FROM [Checks$]", true);
    	      }
    	      catch
    	      {
    	      	MessageBox.Show("Arrived in catch block.");
    	      
    	      }

     

    My program is crashing within the try-block, at the "funcImportSheet" line, and thus the catch block never gets excecuted. (I verified this by stepping through the code). The error messages is "NullReferenceException..Object reference not set to an instance of an object."  The root cause of the problem is that the document read in this case is not an Excel document.

    I'm not very bright. Could someone please explain to me (1) why the catch block isn't catching this exception and (2) what can I do to change that?

     


    Monday, April 25, 2011 12:55 PM

Answers

  • Thanks guys, I found the problem. I am using the portable SharpDevelop IDE instead of Visual Studio (because the IT dept won't give me permission to install VS).

    Turns out the IDE was showing me the current version of the code but was actually running an older version, due to a bug in the IDE. When I used the "rebuild" option I lost the current version (the bad news) but at least I got to the root of the problem finally (the good news).

    It's working fine now.

    Thanks !!!

     

    • Marked as answer by Lie You Wednesday, April 27, 2011 9:20 AM
    Monday, April 25, 2011 1:21 PM

All replies

  • Hello, perhaps you are catching the exception in the funcImportExcelSheetIntoDataTable function?

     

    Try this code:

     

          try
          {
            throw new Exception();
          }
          catch
          {
            MessageBox.Show("Arrived in catch block.");
    
          }
    
    

     

    Hope this helps,

    Miguel.

     

     

    Monday, April 25, 2011 1:06 PM
  • There are scenarios where catch blocks won't be executed (e.g., StackOverflowException), but this doesn't appear to be one of them.  Nothing looks wrong with that code.  However, debugging can produce different results than running.  When you don't attach a debugger, do you see your message box?

    Evan

    Monday, April 25, 2011 1:11 PM
  • On the Debug menu of visual studio choose "Exceptions". Check the box in the Thrown column of the Common Language Runtime Exceptions row.

    Now when you debug you will break on any exception and you will see exactly where it is happening.

    One other thing, it is good practice to catch specific exceptions instead of just doing

    catch

    {

    }

    You should do something more like

    catch(NullReferenceException nre)
    {
    
    }
    catch(Someotherexction... )
    {
    
    }
    etc..
    
    

    Bob - www.crowcoder.com
    Monday, April 25, 2011 1:20 PM
  • You should check whether funcImportExcelSheetIntoDataTable method has try/catch block, and if it is, just use throw operator, in order to pass exception up to calling code.
    Monday, April 25, 2011 1:21 PM
  • Thanks guys, I found the problem. I am using the portable SharpDevelop IDE instead of Visual Studio (because the IT dept won't give me permission to install VS).

    Turns out the IDE was showing me the current version of the code but was actually running an older version, due to a bug in the IDE. When I used the "rebuild" option I lost the current version (the bad news) but at least I got to the root of the problem finally (the good news).

    It's working fine now.

    Thanks !!!

     

    • Marked as answer by Lie You Wednesday, April 27, 2011 9:20 AM
    Monday, April 25, 2011 1:21 PM