Microsoft SDK for Java

Win32Exception Class

The Win32Exception class of the com.ms.dll package provides access to objects that contain error codes and error descriptions for errors set by Microsoft® Win32® functions.

public class Win32Exception extends RuntimeException
{
  // Constructors
  public Win32Exception(int errorcode);
  public Win32Exception(int errorcode, String message);

  // Methods
  public int getErrorCode();
  public static String getErrorDescription(int errorcode);
}

To set an error code, a Win32 function must be imported using the @dll.import directive with the setLastError modifier. This modifier tells the Microsoft VM to capture the error code immediately after invoking the method. To retrieve the error code, you can invoke the com.ms.dll.dllLib.getLastWin32Error method.

Suppose you have imported the Win32 function FindClose, which returns status information through the error code. You can call the FindClose method, retrieve the error code, and wrap the error code in a Win32Exception object as shown by the following example:

import com.ms.dll.*;

boolean successful = FindClose(hFindFile);
if (!successful) {
  Win32Exception e =
        new Win32Exception(DllLib.getLastWin32Error());  
}

Note   You cannot reliably retrieve error codes by using the Win32 function GetLastError. This is because the Microsoft VM, while executing function calls of its own, might overwrite the error code before you retrieve it.

To learn how to import DLL functions such as FindClose, see @dll.import.

Hierarchy

RuntimeException
  |
  +--Win32Exception

© 1999 Microsoft Corporation. All rights reserved. Terms of use.