Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9284529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:55:38+00:00 2026-06-18T18:55:38+00:00

I made a project in VB.NET. If the application I made will produce an

  • 0

I made a project in VB.NET. If the application I made will produce an unwanted error, it will create a text file containing the error. I was able to do this when running it on Visual Studio but it does not work when running the separate application, the executable file found on bin/Debug.

Here’s what I’ve done:

Sub Main(ByVal ParamArray args() As String)
  Try
System.Windows.Forms.Application.Run(New Form1)
  Catch ex As Exception
WriteErrorLogs(ex)
  End Try
End Sub

Sub WriteErrorLogs(Byval ex As Exception)
' create a textfile the write x.Message, x.Source, x.ToString
  Dim dnow As String = Now.ToString
  Dim filename As String = "Error " & removeInvalidChars(dnow)
  Dim saveto As String = New IO.FileInfo("Errors/" & filename).FullName & ".txt"
  Dim title As String = ex.Message
  Dim stacktrce As String = ex.StackTrace

  If Not IO.Directory.Exists(New IO.DirectoryInfo("Errors").FullName) Then IO.Directory.CreateDirectory("Errors")
  Dim fw As New IO.StreamWriter(saveto, False, System.Text.Encoding.UTF8)
  fw.WriteLine(title)
  fw.WriteLine()
  fw.WriteLine(stacktrce)
  fw.Close()
End Sub

Private Function removeInvalidChars(ByRef s As String)
  Dim invalidChars() As Char = "\/:*?""<>|".ToCharArray
  For Each i As Char In invalidChars
    s = s.Replace(i, ".")
  Next
  Return s
End Function

Is there a better solution for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:55:40+00:00Added an answer on June 18, 2026 at 6:55 pm
      Try
          System.Windows.Forms.Application.Run(New Form1)
      Catch ex As Exception
          WriteErrorLogs(ex)
      End Try
    

    Yes, that Catch clause is never going to catch an exception when you run this without a debugger attached. Exceptions that are raised on the UI thread are rerouted and trigger the Application.ThreadException event instead. Which by default displays a dialog, you should have noticed that when you ran it from the bin\Debug directory.

    It works differently when you have a debugger attached, that dialog really gets in the way when you need to debug unhandled exceptions. So the ThreadException event is intentionally disabled and the debugger shows you where your code has crashed. Which will not happen with the code you wrote, now that Catch clause does catch the exception.

    The Catch clause also will not work when your program crashed due to an unhandled exception that was raised on a worker thread, it can only see exceptions on the UI thread.

    You will need a more solid approach, you can get one from the AppDomain.UnhandledException event. Which is raised for any unhandled exception, regardless of what thread it was raised on. Make your code look like this instead:

    Module Module1
        Public Sub Main(ByVal args() As String)
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            If Not System.Diagnostics.Debugger.IsAttached Then
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)
                AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf LogUnhandledExceptions
            End If
            Application.Run(New Form1())
        End Sub
    
        Private Sub LogUnhandledExceptions(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
            Dim ex = DirectCast(e.ExceptionObject, Exception)
            '' Log or display ex.ToString()
            ''...
            Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex))
        End Sub
    End Module
    

    Using Debugger.IsAttached ensures that you can diagnose unhandled exceptions with the debugger. Using Application.SetUnhandledExceptionMode ensures that the dialog is never displayed and all exceptions are logged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made an Area project for my ASP.NET MVC application called 'Admin'. This will
am running the asp.net application...am enhancing the already developed project..i want to create the
I've made a .net 2.0 library project, that results in a dll. I've made
I am trying to run my ASP.NET Web Application Project in my Local IIS
Im working on a web project in ASP .NET MVC 2. In this project
For a new web application project using .NET, an existing library (written in C#)
I'm working on an asp.net application. I have a Domain assembly (containing mostly command
I have made a MVC application that uses the built-in ASP.NET login functions. It
i've made a Class Library project for Asp.Net applications, consisting in some classes plus
Recently my friend gave me a project which is made of vb.net and sql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.