.NET error message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • csommers
    New Member
    • May 2010
    • 1

    .NET error message

    When loading, I get this message. Suggestions on how to handle this?

    Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.



    Any help will be appreciated.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Error: Unhandled exception has occurred in your application.
    You have an exception (an error) that you have not handled.

    How do you fix it? Write better code. Sorry. Generic question gets a generic answer. You need to catch your errors, or better still range-check your values and verify your assumptions so you don't cause the error if it is avoidable.



    I can make some general suggestions that apply to good coding practice.
    • Assume that everything is broken, or at least not ideal.
    • Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
    • Assume that hardware breaks in the middle of what you are doing, so you have to recover.
    • Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
    • Check that files/folders exist, even if you just did a call to make it: You may not have permissions.
    • Don't assume the harddrive has room for what you are doing: They do fill up. Usually right in the middle of you writing to your log file.
    • Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
      • Put a breakpoint on the first line of the method causing trouble.
      • When the code stops there, walk through line by line with F-10.
      • Check the values of your assumptions (looking at the Locals and Automatic variable windows as well as hovering the mouse over the variables in the code (hothelp will popup).
    • Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
    • Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.


    Example:
    Code:
    Graphics g = Graphics.FromImage(m_Undo);
    Presumes that m_Undo must be good (not null)(actually exists)(not in use)(you have permissions)(do esn't time out when accessed). If that assumption fails so does the program, because you can't make anything from a file if the file is null. Get used to validating data and assumptions in your code if you want it to be robust. For example:
    Code:
    if (m_Undo != null)
    {
       bool bSuccess = false;
       // Do your thing here, for example:
       if (myObject != null) bSuccess = true;
       // or
       if (denominator > 0) bSuccess = true;
       // or
       if (MyFunctionReturn != Failed) bSuccess = true;
       // Hurray, your thing worked!
    
       if (bSuccess)
       {
          // Then do this other thing if it worked
       }
       else
       {
          // Then do the failure recovery part / user failure message
       }
    
       return bSuccess; // If you want the calling method to know if it worked.
    }


    May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your look book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books.

    Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
    is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

    C# Cookbooks
    Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

    Microsoft Visual Studio Tip, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
    Has many, many great, real-world tips that I use all the time.

    The tutorials below walk through making an application including inheritance, custom events and custom controls.
    Building an application Part 1
    Building an application part 2



    MSDN C# Developers Center with tutorials
    Welcome to Visual Studio

    Have you seen the MSDN Code Samples for this? The spent a lot of time creating samples and demos. It seems a shame to not use them.
    • Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
    • Arrays: Shows how to use arrays.
    • Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
    • Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
    • COM Interop Part I: Shows how to use C# to interoperate with COM objects.
    • COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
    • Commandline: Demonstrates simple command-line processing and array indexing.
    • Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
    • Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
    • Events: Shows how to declare, invoke, and configure events in C#.
    • Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
    • Generics: Shows how to make generic collection classes that can be used with the foreach statement.
    • Hello World: A Hello World application.
    • Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
    • Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
    • Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
    • Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
    • Nullable: Demonstrates value types, such as double and bool, that can be set to null
    • Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
    • OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
    • Operator Overloading: Shows how user-defined classes can overload operators
    • Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
    • PInvoke: Shows how to call exported DLL functions from C#
    • Properties: Shows how properties are declared and used; also demonstrates abstract properties
    • Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
    • Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
    • Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
    • Structs: Shows how to use structs in C#.
    • Threading: Demonstrates various thread activities such as creating and executing a thread, synchronizing threads, interacting between threads, and using a thread pool
    • Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
    • User Conversions: Shows how to define conversions to and from user-defined types
    • Versioning: Demonstrates versioning in C# by using the override and new keywords
    • XML Documents: Shows how to document code by using XML
    • Yield: Demonstrates how to use the yield keyword to filter items in a collection

    Comment

    Working...