简体   繁体   中英

System.ArgumentOutOfRangeException when calling Method. How do I find out where the error occurs?

I designed a program with Windows Forms Designer and added functionality that gets executed when pushing a certain Button. After executing my program and clicking on said button I get the following Error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

The weird part is that it tells me the error happens when I call the method; as in:

private void btPPTX_Click(object sender, EventArgs e)
{
    PptxConverter.Generate();
}

I had the same error before but it showed me the exact line where it was happening(Inside of the Generate-Method). I corrected my error but now I don't even know where to start looking. The Generate-Method is very extensive and involves a whole set of other methods so it's tough to find the error or post the whole code here. Is there any effective way to find the error or do I have to go through the code line by line to find it?
I'm using Visual Studio Community 2015

The StackTrace is one of your ways to investigate exception in a method graph (nested calls):

try
{
   PptxConverter.Generate();
}
catch(Exception ex)
{
   Console.WriteLine(ex);
}

When you print the exception object, you get the exception message and the stack trace which is a detailed call hierarchy that leads you to the exact Method(with line number sometimes) where the last exception was thrown.

Given a method Main that calls A that calls B that calls C (which throws the exception) Stack-trace looks like this:

/*
    System.Exception: Some Error Message
    at ProjectName.Program.C() in C:\Users\User\Documents\Visual Studio 2015\Proj
ects\ProjectName\ProjectName\Program.cs:line 87
    at ProjectName.Program.B() in C:\Users\User\Documents\Visual Studio 2015\Proj
ects\ProjectName\ProjectName\Program.cs:line 82
    at ProjectName.Program.A() in C:\Users\User\Documents\Visual Studio 2015\Proj
ects\ProjectName\ProjectName\Program.cs:line 77
    at ConsoleTest.Program.Main(String[] args) in C:\Users\User\Documents\Visual
Studio 2015\Projects\ProjectName\ProjectName\Program.cs:line 52
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM