1 //------------------------------------------------------------------------------
2 // <copyright file="DataSetUtil.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
11 using System.Data.DataSetExtensions;
12 using System.Diagnostics;
14 internal static class DataSetUtil
17 internal static void CheckArgumentNull<T>(T argumentValue, string argumentName) where T : class
19 if (null == argumentValue)
21 throw ArgumentNull(argumentName);
27 private static T TraceException<T>(string trace, T e)
29 Debug.Assert(null != e, "TraceException: null Exception");
32 //Bid.Trace(trace, e.ToString()); // will include callstack if permission is available
37 private static T TraceExceptionAsReturnValue<T>(T e)
39 return TraceException("<comm.ADP.TraceException|ERR|THROW> '%ls'\n", e);
44 internal static ArgumentException Argument(string message)
46 return TraceExceptionAsReturnValue(new ArgumentException(message));
49 internal static ArgumentNullException ArgumentNull(string message)
51 return TraceExceptionAsReturnValue(new ArgumentNullException(message));
54 internal static ArgumentOutOfRangeException ArgumentOutOfRange(string message, string parameterName)
56 return TraceExceptionAsReturnValue(new ArgumentOutOfRangeException(parameterName, message));
59 internal static InvalidCastException InvalidCast(string message)
61 return TraceExceptionAsReturnValue(new InvalidCastException(message));
64 internal static InvalidOperationException InvalidOperation(string message)
66 return TraceExceptionAsReturnValue(new InvalidOperationException(message));
69 internal static NotSupportedException NotSupported(string message)
71 return TraceExceptionAsReturnValue(new NotSupportedException(message));
75 #region new EnumerationValueNotValid
76 static internal ArgumentOutOfRangeException InvalidEnumerationValue(Type type, int value)
78 return ArgumentOutOfRange(Strings.DataSetLinq_InvalidEnumerationValue(type.Name, value.ToString(System.Globalization.CultureInfo.InvariantCulture)), type.Name);
81 static internal ArgumentOutOfRangeException InvalidDataRowState(DataRowState value)
86 case DataRowState.Detached:
87 case DataRowState.Unchanged:
88 case DataRowState.Added:
89 case DataRowState.Deleted:
90 case DataRowState.Modified:
91 Debug.Assert(false, "valid DataRowState " + value.ToString());
95 return InvalidEnumerationValue(typeof(DataRowState), (int)value);
98 static internal ArgumentOutOfRangeException InvalidLoadOption(LoadOption value)
103 case LoadOption.OverwriteChanges:
104 case LoadOption.PreserveChanges:
105 case LoadOption.Upsert:
106 Debug.Assert(false, "valid LoadOption " + value.ToString());
110 return InvalidEnumerationValue(typeof(LoadOption), (int)value);
114 // only StackOverflowException & ThreadAbortException are sealed classes
115 static private readonly Type StackOverflowType = typeof(System.StackOverflowException);
116 static private readonly Type OutOfMemoryType = typeof(System.OutOfMemoryException);
117 static private readonly Type ThreadAbortType = typeof(System.Threading.ThreadAbortException);
118 static private readonly Type NullReferenceType = typeof(System.NullReferenceException);
119 static private readonly Type AccessViolationType = typeof(System.AccessViolationException);
120 static private readonly Type SecurityType = typeof(System.Security.SecurityException);
122 static internal bool IsCatchableExceptionType(Exception e)
124 // a 'catchable' exception is defined by what it is not.
125 Type type = e.GetType();
127 return ((type != StackOverflowType) &&
128 (type != OutOfMemoryType) &&
129 (type != ThreadAbortType) &&
130 (type != NullReferenceType) &&
131 (type != AccessViolationType) &&
132 !SecurityType.IsAssignableFrom(type));