// Set a particular error number in the program's error object. private static void SetErrorNumber(int number) { ErrObject error = Information.Err(); error.Clear(); error.Number = number; }
// Throw an exception by number. public static void ThrowException(int hr) { Exception exception = ErrObject.CreateExceptionFromNumber(hr, null); SetErrorNumber(hr); throw exception; }
// Create a new project error. public static Exception CreateProjectError(int hr) { ClearProjectError(); hr = ErrObject.HResultToNumber(hr); Exception e = ErrObject.CreateExceptionFromNumber(hr, null); Information.Err().Number = hr; return(e); }
/// <summary>Performs the work for the <see langword="Raise" /> method of the <see langword="Err" /> object. A helper method.</summary> /// <param name="hr">An integer value that identifies the nature of the error. Visual Basic errors are in the range 0–65535; the range 0–512 is reserved for system errors; the range 513–65535 is available for user-defined errors.</param> /// <returns>An <see cref="T:System.Exception" /> object.</returns> public static Exception CreateProjectError(int hr) { ErrObject errObject = Information.Err(); errObject.Clear(); int num = errObject.MapErrorNumber(hr); return(errObject.CreateException(hr, Utils.GetResourceString((vbErrors)num))); }
static void Main(string[] args) { ErrObject err = Information.Err(); // Definitions const int PART1_LENGTH = 5; string sPart1 = "Some value"; int vbObjectError = 123; double d; if (sPart1.Length != PART1_LENGTH) { err.Raise(vbObjectError, null, "Part 1 must be " + PART1_LENGTH); } else if (double.TryParse(sPart1, out d)) { err.Raise(vbObjectError, null, "Part 1 must be numeric"); } }
// Throw a particular exception, after setting the error number. private static void ThrowExceptionInternal(Exception exception) { SetErrorNumber(ErrObject.GetNumberForException(exception)); throw exception; }