An exception is any error condition or unexpected behavior encountered by an executing program. Whenever an error occurs, an object that derives from the System.Exception class is created to represent the exception.
Whenever you contact technical support regarding an exception, please include the exception's type, message, stack trace and all InnerException objects. This information will allow for a quick diagnostis of the issue. Here is sample code that collects the required information:
| C# |
Copy Code |
|---|---|
try { /* ...Code that triggers an exception... */ } catch( Exception exception ) { // Output some information about the exception System.Diagnostics.Debug.WriteLine( "-->{0}: {1}\n{2}", exception.GetType().Name, exception.Message, exception.StackTrace ); // Fetch the inner exception exception = exception.InnerException; // While there is an exception while( exception != null ) { // Output some information about it System.Diagnostics.Debug.WriteLine( "-->Inner exception: {0}: {1}\n{2}", exception.GetType().Name, exception.Message, exception.StackTrace ); // Fetch the inner exception exception = exception.InnerException; } } | |
| VB.NET |
Copy Code |
|---|---|
Try ' ...Code that triggers an exception... Catch exception As Exception ' Output some information about the exception System.Diagnostics.Debug.WriteLine("-->{0}: {1}" & Constants.vbLf & "{2}", exception.GetType().Name, exception.Message, exception.StackTrace) ' Fetch the inner exception exception = exception.InnerException ' While there is an exception Do While exception IsNot Nothing ' Output some information about it System.Diagnostics.Debug.WriteLine("-->Inner exception: {0}: {1}" & Constants.vbLf & "{2}", exception.GetType().Name, exception.Message, exception.StackTrace) ' Fetch the inner exception exception = exception.InnerException Loop ' OPTIONAL: Depending on the architecture of the application, it might be a good idea to rethrow the exception 'Throw End Try | |
The exceptions that can be thrown by Xceed Zip for .NET are handled in the same manner as regular run-time exceptions using Try/Catch blocks. It is possible to intercept the exceptions before they are thrown using the ItemException event in order to try and recover from the exception before it is actually thrown.
The following table provides a listing of all the custom exceptions that can be thrown when an error occurs:
| Exception | Description |
|---|---|
| FileSystemException | Exception that is thrown when an error occurs in the Xceed.FileSystem namespace. |
| FileSystemInternalException | Exception that is thrown when something unexpected occurs in the Xceed.FileSystem namespace. |
| FileSystemIOException | Exception that is thrown when an IO error occurs while reading from or writing to an AbstractFile object. |
| ItemAlreadyExistsException | Exception that is thrown when performing an operation on a FileSystemItem object that already exists. |
| ItemDoesNotExistException | Exception that is thrown when performing an operation on a FileSystemItem object that does not yet exist. |
| ItemIsReadOnlyException | Exception that is thrown when performing an operation on a FileSystemItem object that is read only. |
| ItemIsRootFolderException | Exception that is thrown when performing an operation on a FileSystemItem object that is a root folder that does not permit that operation. |
| InvalidDecryptionPasswordException | Exception that is thrown when the data could not be decrypted with the supplied decryption password. |
| InvalidZipStructureException | Exception that is thrown when the internal structure of a ZIP file is invalid. |
| InvalidSfxModuleException | Exception that is thrown when an invalid SFX binary is provided. |
| ZipException | Exception that is thrown when an error occurs in the Xceed.Zip namespace. |
| ZipIOException | Exception that is thrown when an IO exception occurs while reading from or writing to the ZIP file. |
| QuickZipException | Exception that is thrown when an error occurs in a method of the QuickZip class |
| InvalidTarStructureException | Exception that is thrown when the internal structure of a tar file is invalid. This exception is not available in Xceed's .NET Compact Framework products. |
| InvalidGZipStructureException | Exception that is thrown when the internal structure of a gzip file is invalid. This exception is not available in Xceed's .NET Compact Framework products. |
| CompressionException | Exception that is thrown when there is a problem compressing or decompressing data. |
| CompressionInternalException | Exception that is thrown when something unexpected occurs while compressing or decompressing data. |
The ZipException class derives directly from the FileSystemException class. All of the other exception classes defined within the Xceed.Zip namespace derive from the ZipException class.
The following example demonstrates how to use a Try/Catch statement when calling the Delete method on a ZippedFile that is contained within a corrupted zip file:
| C# |
Copy Code |
|---|---|
try { // Get a zip file on disk AbstractFile zipFile = new DiskFile( @"C:\Test.zip" ); // Get a file located inside the zip archive AbstractFile zippedFile = new ZippedFile( zipFile, @"\file.txt" ); zippedFile.Delete(); } // Catch a specific exception type catch( ItemDoesNotExistException ) { // We choose to ignore this error for this specific operation } // Catch all other exception types catch( Exception exception ) { // Output some information about it System.Diagnostics.Debug.WriteLine( "-->{0}: {1}\n{2}", exception.GetType().Name, exception.Message, exception.StackTrace ); // Fetch the inner exception exception = exception.InnerException; // While there is an exception while( exception != null ) { // Output some information about it System.Diagnostics.Debug.WriteLine( "-->Inner exception: {0}: {1}\n{2}", exception.GetType().Name, exception.Message, exception.StackTrace ); // Fetch the inner exception exception = exception.InnerException; } // OPTIONAL: Depending on the architecture of the application, it might be a good idea to rethrow the exception //throw; } | |
| VB.NET |
Copy Code |
|---|---|
Try ' Get a zip file on disk Dim zipFile As AbstractFile = New DiskFile("C:\Test.zip") ' Get a file located inside the zip archive Dim zippedFile As AbstractFile = New ZippedFile(zipFile, "\file.txt") zippedFile.Delete() ' Catch a specific exception type Catch e1 As ItemDoesNotExistException ' We choose to ignore this error for this specific operation ' Catch all other exception types Catch exception As Exception ' Output some information about it System.Diagnostics.Debug.WriteLine("-->{0}: {1}" & Constants.vbLf & "{2}", exception.GetType().Name, exception.Message, exception.StackTrace) ' Fetch the inner exception exception = exception.InnerException ' While there is an exception Do While exception IsNot Nothing ' Output some information about it System.Diagnostics.Debug.WriteLine("-->Inner exception: {0}: {1}" & Constants.vbLf & "{2}", exception.GetType().Name, exception.Message, exception.StackTrace) ' Fetch the inner exception exception = exception.InnerException Loop ' OPTIONAL: Depending on the architecture of the application, it might be a good idea to rethrow the exception 'Throw End Try | |
The main questions you should ask yourself when handling exceptions are: