Не получается разобраться с исключениями - C#
Формулировка задачи:
дошел до исключений, че-то не так легко пошло понимаем принципа работы
как работает
try-catch мне понятно
но зачем использовать оператор throw не совсем понятно...
могли вы привести пример, где без этого оператора не обойтись
пример желательно попроще =)
Решение задачи: «Не получается разобраться с исключениями»
textual
Листинг программы
[SecuritySafeCritical] internal void Init(string path, FileMode mode, FileAccess access, int rights, bool useRights, FileShare share, int bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, string msgPath, bool bFromProxy, bool useLongPath) { int num1; if (path == null) { throw new ArgumentNullException("path", Environment.GetResourceString("ArgumentNull_Path")); } if (path.Length == 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath")); } FileSystemRights fileSystemRight = rights; this._fileName = msgPath; this._exposedHandle = false; FileShare fileShare = share & (FileShare.Read | FileShare.Write | FileShare.ReadWrite | FileShare.Delete); string str1 = null; if (mode < 1 || mode > 6) { str1 = "mode"; } if (str1 != null) { throw new ArgumentOutOfRangeException(str1, Environment.GetResourceString("ArgumentOutOfRange_Enum")); } if (options != FileOptions.None && (options & 67092479) != FileOptions.None) { throw new ArgumentOutOfRangeException("options", Environment.GetResourceString("ArgumentOutOfRange_Enum")); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum")); } if ((!useRights && (access & FileAccess.Write) == 0 || useRights && (fileSystemRight & FileSystemRights.Write) == 0) && (mode == FileMode.Truncate || mode == FileMode.CreateNew || mode == FileMode.Create) || mode == FileMode.Append) { if (!useRights) { throw new ArgumentException(Environment.GetResourceString(object[] objArray1[1] = access, objArray1)); } throw new ArgumentException(Environment.GetResourceString(object[] objArray2[1] = fileSystemRight, objArray2)); } if (useRights && mode == FileMode.Truncate) { if (fileSystemRight == FileSystemRights.Write) { useRights = 0; access = FileAccess.Write; } throw new ArgumentException(Environment.GetResourceString(object[] objArray3[1] = fileSystemRight, objArray3)); } if (!useRights) { num1 = access == FileAccess.Read; } else { num1 = rights; } int num2 = (useLongPath ? Path.MaxLongPath : Path.MaxPath); string str2 = Path.NormalizePath(path, true, num2); this._fileName = str2; if (str2.StartsWith("\\\\.\\", StringComparison.Ordinal)) { throw new ArgumentException(Environment.GetResourceString("Arg_DevicesNotSupported")); } FileIOPermissionAccess fileIOPermissionAccess = FileIOPermissionAccess.NoAccess; if (!useRights && (access & FileAccess.Read) != 0 || useRights && (fileSystemRight & FileSystemRights.ReadAndExecute) != 0) { if (mode == FileMode.Append) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidAppendMode")); } fileIOPermissionAccess = fileIOPermissionAccess | FileIOPermissionAccess.Read; } if ((useRights || (access & FileAccess.Write) == 0) && (useRights || (fileSystemRight & (FileSystemRights.WriteData | FileSystemRights.CreateFiles | FileSystemRights.AppendData | FileSystemRights.CreateDirectories | FileSystemRights.WriteExtendedAttributes | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.WriteAttributes | FileSystemRights.Delete | FileSystemRights.ChangePermissions | FileSystemRights.TakeOwnership | FileSystemRights.Write)) == 0) && (useRights || (fileSystemRight & FileSystemRights.Synchronize) == 0 || mode != FileMode.OpenOrCreate) || mode == FileMode.Append) { fileIOPermissionAccess = fileIOPermissionAccess | FileIOPermissionAccess.Append; } AccessControlActions accessControlAction = ((secAttrs == null ? AccessControlActions.None : secAttrs.pSecurityDescriptor != (UIntPtr)0) ? AccessControlActions.Change : AccessControlActions.None); new FileIOPermission(string[] strArrays1 = new string[1], strArrays1[0] = str2, strArrays1, false, false).Demand(); share = share & (FileShare.Read | FileShare.Write | FileShare.ReadWrite | FileShare.Delete); if (mode == FileMode.Append) { mode = FileMode.OpenOrCreate; } if (FileStream._canUseAsync) { if ((options & FileOptions.Asynchronous) != FileOptions.None) { this._isAsync = true; } else { options = options & (FileOptions.WriteThrough | FileOptions.RandomAccess | FileOptions.DeleteOnClose | FileOptions.SequentialScan | FileOptions.Encrypted); } } int num3 = options; num3 = num3 | 1048576; int num4 = Win32Native.SetErrorMode(1); try { string str3 = str2; if (useLongPath) { str3 = Path.AddLongPathPrefix(str3); } this._handle = Win32Native.SafeCreateFile(str3, num1, share, secAttrs, mode, num3, Win32Native.NULL); if (this._handle.IsInvalid) { int lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error == 3 && str2.Equals(Directory.InternalGetDirectoryRoot(str2))) { lastWin32Error = 5; } bool flag1 = false; if (!bFromProxy) { new FileIOPermission(string[] strArrays2[0] = this._fileName, strArrays2, false, false).Demand(); flag1 = true; } try { } catch (SecurityException) { } if (flag1) { __Error.WinIOError(lastWin32Error, this._fileName); } else { __Error.WinIOError(lastWin32Error, msgPath); } } } finally { Win32Native.SetErrorMode(num4); } int fileType = Win32Native.GetFileType(this._handle); if (fileType != 1) { this._handle.Close(); throw new NotSupportedException(Environment.GetResourceString("NotSupported_FileStreamOnNonFiles")); } if (this._isAsync) { bool flag2 = false; new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); try { flag2 = ThreadPool.BindHandle(this._handle); } finally { CodeAccessPermission.RevertAssert(); if (!flag2) { this._handle.Close(); } } if (!flag2) { throw new IOException(Environment.GetResourceString("IO.IO_BindHandleFailed")); } } if (!useRights) { this._canRead = (access & FileAccess.Read) != 0; this._canWrite = (access & FileAccess.Write) != 0; } else { this._canRead = (fileSystemRight & FileSystemRights.ReadData) != 0; this._canWrite = (fileSystemRight & FileSystemRights.WriteData ? FileSystemRights.ReadData : (fileSystemRight & FileSystemRights.AppendData) != 0); } this._canSeek = true; this._isPipe = false; this._pos = (long)0; this._bufferSize = bufferSize; this._readPos = 0; this._readLen = 0; this._writePos = 0; if (mode == FileMode.Append) { this._appendStart = this.SeekCore((long)0, SeekOrigin.End); return; } this._appendStart = (long)-1; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д