Leadtools TWAIN (Leadtools.Twain assembly) Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.30
TwainException Class
See Also  Members  
Leadtools.Twain Namespace : TwainException Class



The exception that is thrown when a TWAIN error occurs.

Object Model

TwainException Class

Syntax

Visual Basic (Declaration) 
<SerializableAttribute()>
Public Class TwainException 
   Inherits Leadtools.LeadtoolsException
   Implements _ExceptionISerializable 
Visual Basic (Usage)Copy Code
Dim instance As TwainException
C++/CLI 
[SerializableAttribute()]
public ref class TwainException : public Leadtools.LeadtoolsException, _ExceptionISerializable  

Example

Visual BasicCopy Code
Public Sub _twainSession_AcquirePage(ByVal sender As Object, ByVal e As TwainAcquirePageEventArgs)
   Application.DoEvents()
   If Not e.Image Is Nothing Then
      e.Image.Dispose()
   End If

   e.Cancel = False
End Sub


Public Sub TwainExceptionExample(ByVal parent As IWin32Window)
   Dim session As TwainSession = Nothing

   Try
      ' initialize a new TWAIN session
      session = New TwainSession()
      session.Startup(parent, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None)
      AddHandler session.AcquirePage, AddressOf _twainSession_AcquirePage

      ' acquire a page, if paper jam, allow the user to retry
      Dim done As Boolean = False
      Do While Not done
         Try
            Dim res As DialogResult = session.Acquire(TwainUserInterfaceFlags.Modal Or TwainUserInterfaceFlags.Show)
            MessageBox.Show("Success")
            done = True
         Catch ex As TwainException
            If ex.Code = TwainExceptionCode.PaperJam Then
               If MessageBox.Show("Paper jam.  Fix and retry?", "TWAIN", MessageBoxButtons.YesNo) = DialogResult.No Then
                  done = True
               End If
            Else
               ' other error, propogate
               Throw ex
            End If
         End Try
      Loop
   Catch ex As TwainException
      MessageBox.Show(String.Format("Twain error:{0}Code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message))
   Catch ex As Exception
      MessageBox.Show(String.Format("Other error: Message:{0}", ex.Message))
   Finally
      If Not session Is Nothing Then
         session.Shutdown()
      End If
   End Try
End Sub
C#Copy Code
public void _twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
   Application.DoEvents();
   if(e.Image != null)
      e.Image.Dispose();

   e.Cancel = false;
}


public void TwainExceptionExample(IWin32Window parent)
{
   TwainSession session = null;

   try
   {
      // initialize a new TWAIN session
      session = new TwainSession();
      session.Startup(parent, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None);
      session.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(_twainSession_AcquirePage);

      // acquire a page, if paper jam, allow the user to retry
      bool done = false;
      while(!done)
      {
         try
         {
            DialogResult res = session.Acquire(TwainUserInterfaceFlags.Modal | TwainUserInterfaceFlags.Show);
            MessageBox.Show("Success");
            done = true;
         }
         catch(TwainException ex)
         {
            if(ex.Code == TwainExceptionCode.PaperJam)
            {
               if(MessageBox.Show("Paper jam.  Fix and retry?", "TWAIN", MessageBoxButtons.YesNo) == DialogResult.No)
                  done = true;
            }
            else
            {
               // other error, propogate
               throw ex;
            }
         }
      }
   }
   catch(TwainException ex)
   {
      MessageBox.Show(string.Format("Twain error:{0}Code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message));
   }
   catch(Exception ex)
   {
      MessageBox.Show(string.Format("Other error: Message:{0}", ex.Message));
   }
   finally
   {
      if(session != null)
         session.Shutdown();
   }
}

Remarks

The TwainException class defines a Code property that you can examine to determine what caused the error.

Default messages are implemented for each Code, you can however modify any message through the GetCodeMessage and SetCodeMessage methods.

Inheritance Hierarchy

System.Object
   System.Exception
      Leadtools.LeadtoolsException
         Leadtools.Twain.TwainException

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 2000, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7

See Also