Hi everyone!

I have a c# app, in which I get a FormatException. The problem is that whatever I do to trace it doesn't succeed.

I have tried using breakpoints, try-catch blocks in various parts of the code that I would expect it to occur, putting

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

in the Main() and still cannot trace it back. Any suggestions? I am out of ideas.

Thanks

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi,

Why don't you try to do it the old fashion way ... comment out suspect code blocks until it doesn't happen anymore :)

What do you mean can't trace it back? I'm guessing you're running the application inside of VS and have the project output set to Debug? Hit CTRL+D, then E. This will bring up the exceptions window. The checkboxes in the right column should all be checked. For trying to catch this exception also check the "Thrown" option for "Common Language runtime Exceptions". To catch unhandled exceptions and generating a bug report for the application before you crash out can be done like this:

Program.cs

using System;
using System.Windows.Forms;

namespace daniweb
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
      Application.Run(new frmSerkan());
    }

    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
      System.Text.StringBuilder sb = new System.Text.StringBuilder();
      Exception ex = e.Exception;
      while (ex != null)
      {
        sb.AppendLine("Message: " + ex.Message);
        sb.AppendLine("Source: " + ex.Source);
        sb.AppendLine("Stack: ");
        sb.AppendLine(ex.StackTrace);
        ex = ex.InnerException;
      }
      //Dont really use a message box. This will be huge!
      Console.WriteLine(sb.ToString());
      if (System.Diagnostics.Debugger.IsAttached)
        System.Diagnostics.Debugger.Break();
    }
  }
}

Simulating it:

private void button13_Click(object sender, EventArgs e)
{
  throw new NotImplementedException("Crashing!");
}

Two things to consider:
1) Also make sure you use (code) when ending a code block.
2) Make sure your project has .pdb files in the same directory as the EXE. These are the debugging symbols. They should have the same create date as the assembly.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thanks Sknake. I'll try it (and sorry for code)

Edited 3 Years Ago by happygeek: fixed formatting

Comments
A for effort!

Next time if you notice that after you post you can go back and hit "Edit Post" under your original post and fix it. You can only edit a post for ~45 minutes after you create it. I think the time as passed already but you can give it a shot :)

Good luck!

This article has been dead for over six months. Start a new discussion instead.