Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

Catch printing exceptions

I have a windows forms application in Visual Basic.net 2005. I have used the application events to catch all unhandled application exceptions and all unhandled GUI exceptions. I would like to specifically catch all printing exceptions in the thread exception handler so I can handle them differently, ie rather than shutdown, display message to user then cancel print command.

What code can I use to catch print exceptions?

Thanks.
Partial Friend Class MyApplication
 
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            AddHandler System.Windows.Forms.Application.ThreadException, AddressOf OnGuiUnhandedException
        End Sub
        Private Sub OnGuiUnhandedException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
            HandleUnhandledException(e.Exception)
        End Sub
 
        Private Sub HandleUnhandledException(ByVal o As Object)
            Dim e As Exception = TryCast(o, Exception)
 
            If e IsNot Nothing Then
                ' Report System.Exception info 
                Dim message As String = ""
                message = "  Message: " & e.InnerException.ToString & "  " & e.Message & vbCrLf & _
                  "  Source: " & e.Source & vbCrLf & vbCrLf & _
                  "  Stack Trace:" & vbCrLf
                Dim trace As String
 
                ' Accessing an exception objects StackTrace could cause an exception
                ' thus we need to wrap the call in its own Try, Catch block.
                Try
                    trace = e.StackTrace()
                Catch stExp As Security.SecurityException
                    trace = "Unable to access stack trace due to security restrictions."
                Catch stExp As Exception
                    trace = "Unable to access stack trace."
                End Try
 
                message = message & trace
                'MessageBox.Show("I caught a GUI exception")
                My.Application.Log.WriteException(e, _
                    TraceEventType.Critical, _
                    "Application shut down at " & _
                    My.Computer.Clock.GmtTime.ToString & vbCrLf & message)
                'MessageBox.Show("An unexpected error has occurred. The application will shut down.")
            End If
 
            MessageBox.Show("An unhandled exception occurred " & "and the application is shutting down.")
            Environment.[Exit](1)
            ' Shutting down 
        End Sub
        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            'Exception logging code copied from "How to: Log Exceptions in Visual Basic "
            Dim message As String = ""
            message = "  Message: " & e.Exception.Message & e.Exception.InnerException.ToString & "  " & vbCrLf & _
              "  Source: " & e.Exception.Source & vbCrLf & vbCrLf & _
              "  Stack Trace:" & vbCrLf
            Dim trace As String
 
            ' Accessing an exception objects StackTrace could cause an exception
            ' thus we need to wrap the call in its own Try, Catch block.
            Try
                trace = e.Exception.StackTrace()
            Catch stExp As Security.SecurityException
                trace = "Unable to access stack trace due to security restrictions."
            Catch stExp As Exception
                trace = "Unable to access stack trace."
            End Try
 
            message = message & trace
            MessageBox.Show(message)
            My.Application.Log.WriteException(e.Exception, _
                TraceEventType.Critical, _
                "Application shut down at " & _
                My.Computer.Clock.GmtTime.ToString & vbCrLf & message)
            MessageBox.Show("An unexpected error has occurred. The application will shut down.")
            'It is the default that the application closes after this event has occured
        End Sub
    End Class

Open in new window

0
GIANTOCR
Asked:
GIANTOCR
1 Solution
 
CodeCruiserCommented:
As the System.Printing namespace shows, there are many specific exceptions related to printing such as
PrintingNotSupportedException
PrintJobException
PrintQueueException

This link provides more information

http://msdn.microsoft.com/en-us/library/system.printing.aspx
0
 
GIANTOCRAuthor Commented:
Thanks for your help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now