Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    VB NET: Catch impazzito?

    salve a tutti!

    ho un problema: il mio programma controlla se un file excel contiene un foglio con un determinato nome e crea un file x discriminare i casi in cui il file è del formato corretto o meno...poi il file originario lo sposta in un'altra cartella...

    in realtà su altri pc funziona tutto ok ma sul mio, girando in debug, sembra entrare nel catch del main ma esegue solo l'ultima istruzione del catch (qualsiasi essa sia!!)ho riavviato sia visual che il SO ma sempre stesso problema...che devo fare??qlc sà che potrebbe essere??

    posto qui il codice:

    Imports System.IO
    Imports System.Diagnostics

    Module Loader

    Const TableName As String = "DEXTER_DATA"
    Const BackupDir As String = "c:\dirbackup"


    Sub Main(ByVal args As String())
    Dim wFileRes As FileStream
    Dim wSource As String = "SDS2E4 Directory Watcher"
    Dim wLog As String = "SDS2E4"
    Dim wFileName As String = Nothing
    Dim wFileNameWithoutPath As String
    Dim wIndex As Long

    If (args.Length = 1) Then
    wFileName = args(1)

    Try
    If (IsCorrectExcelFile(wFileName)) Then
    wFileRes = File.Create(wFileName + "_Excel")
    Else
    wFileRes = File.Create(wFileName + "_NONExcel")
    End If
    wFileRes.Close()

    'sposto il file elaborato in una directory di backup
    If (Not Directory.Exists(BackupDir)) Then
    Directory.CreateDirectory(BackupDir)
    End If
    wIndex = wFileName.LastIndexOf("\")
    wFileNameWithoutPath = wFileName.Substring(wIndex)
    File.Move(wFileName, BackupDir + wFileNameWithoutPath)

    Catch ex As Exception
    CreateEventLog(wSource, wLog)
    EventLog.WriteEntry(wSource, "Errore Elaborando il File: " + ex.Message, EventLogEntryType.Error)
    End Try

    Else
    CreateEventLog(wSource, wLog)
    EventLog.WriteEntry(wSource, "Nessun File Da Elaborare", EventLogEntryType.Error)
    End If

    End Sub

    Private Sub CreateEventLog(ByVal aiSource As String, ByVal aiLog As String)
    If (Not EventLog.SourceExists(aiSource)) Then
    EventLog.CreateEventSource(aiSource, aiLog)
    End If
    End Sub
    Private Function IsCorrectExcelFile(ByVal aiFileName As String) As Boolean
    Dim wExcelApplication As Object = Nothing
    Dim wExcelWorkbooks As Object = Nothing
    Dim wExcelWorkbook As Object = Nothing
    Dim wSheets As Object = Nothing
    Dim wSheet As Object = Nothing
    Dim i As Long
    Dim wNumPag As Long

    Try
    wExcelApplication = CreateObject("Excel.Application")
    'nasconde messaggi di allerta di excel
    wExcelApplication.DisplayAlerts = False
    wExcelWorkbooks = wExcelApplication.Workbooks
    wExcelWorkbook = wExcelWorkbooks.Open(aiFileName)
    wSheets = wExcelWorkbook.Worksheets

    'controllo se è un file excel valido
    wNumPag = wSheets.count
    For i = 0 To wNumPag - 1
    wSheet = wSheets.Item(i + 1)
    If (wSheet.Name = TableName) Then
    IsCorrectExcelFile = True
    Exit For
    Else
    IsCorrectExcelFile = False
    End If
    Next

    Catch wEx As Exception
    IsCorrectExcelFile = False

    Finally
    'close excel application
    NAR(wSheet)
    NAR(wSheets)
    If (Not wExcelWorkbook Is Nothing) Then
    wExcelWorkbook.Close(False)
    End If
    NAR(wExcelWorkbook)
    NAR(wExcelWorkbooks)
    If (Not wExcelApplication Is Nothing) Then
    wExcelApplication.Quit()
    End If
    NAR(wExcelApplication)
    End Try

    End Function

    'cancella tutte le variabili per la pulizia di excel
    Private Sub NAR(ByVal o As Object)
    Try
    System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
    Catch
    Finally
    o = Nothing
    End Try
    End Sub

    End Module

  2. #2
    Scusa, ma non ho capito la domanda. Sembra entrare nel catch o ci entra davero? In teoria, se non hai impostato la commandline, dovrebbe endare sempre nel catch. In questo caso esegue l'ultima istruzione del catch perchè probabilmente ha già creao il log, ma non può saltare le istruzioni ed eseguire solo l'ultima.

  3. #3
    ci entra ma diciamo per finta xchè nel catch:

    Catch ex As Exception
    CreateEventLog(wSource, wLog)
    EventLog.WriteEntry(wSource, "Errore Elaborando il File: " + ex.Message, EventLogEntryType.Error)
    End Try


    passa subito ad eseguire l'istruzione:

    EventLog.WriteEntry(wSource, "Errore Elaborando il File: " + ex.Message, EventLogEntryType.Error)

    anzichè partire dall'inizio del catch e se guardo qnt vale ex mi dice addirittura che non è dichiarata (poichè, secondo me, è come se vedesse questa istruzione fuori dal catch...)

    sul pc di un mio collega funziona corretamente mentre sul mio no!!

    scusa am cosa intendi con questo"In teoria, se non hai impostato la commandline, dovrebbe endare sempre nel catch. In questo caso esegue l'ultima istruzione del catch perchè probabilmente ha già creao il log, ma non può saltare le istruzioni ed eseguire solo l'ultima."

    nn và nel catch solo se cattura un errore?

  4. #4
    Prova a ricompilare tutto il progetto... magari l'eseguibile compilato risale ad una compilazione precedente che non includeva la prima istruzione.
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    già fatto...o anche riavviato ma nulla...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.