c# - Handling exceptions from a method that could fire multiple exceptions -


i have api class using memorystream , gzipstream classes compress , decompress string byte array.

using these 2 classes number of exceptions may throws , wondering best method of handling thrown exception api. in case be better wrap each of exception own custom exception or preferable capture each individual exception in calling code?

i suppose question not limited particular use case more general exception handling best practices.

/// <summary> /// compress string using sharplibzip gzip compression routines /// </summary> /// <param name="s">string object compress</param> /// <returns>a gzip compressed byte array of passed in string</returns> /// <exception cref="helper.core.compression.stringcompressionexception">throw when compression memory stream fails </exception> /// <exception cref="system.argumentnullexception">throw when string parameter null</exception> /// <exception cref="system.argumentexception">throw when string parameter empty</exception> public async task<byte[]> compressstringasync(string s) {     if (s == null) throw new argumentnullexception("s");     if (string.isnullorwhitespace(s)) throw new argumentexception("s");      byte[] compressed = null;      try     {         using (memorystream outstream = new memorystream())         {             using (gzipstream tinystream = new gzipstream(outstream,compressionmode.compress))             {                 using (memorystream memstream = new memorystream(encoding.utf8.getbytes(s)))                 {                     await memstream.copytoasync(tinystream);                 }             }             compressed = outstream.toarray();         }         return compressed;     }     catch (argumentnullexception ex)     {         throw new stringcompressionexception("argument null", ex);     }     catch (encoderfallbackexception ex)     {         throw new stringcompressionexception("stream encoding failure", ex);     }     catch (argumentexception ex)     {         throw new stringcompressionexception("argument not valid", ex);     }     catch (objectdisposedexception ex)     {         throw new stringcompressionexception("a stream disposed", ex);     }     catch (notsupportedexception ex)     {         throw new stringcompressionexception("action not supported", ex);     } } 

here post on catching base exceptions.

you're taking advantage of "inner exception" functionality of exception base class in example code show, why not take way?

you don't need re-write message of inner exception, wrap exception in stringcompressionexception class. if client catches exception wants know more detailed information failure, can check inner exception.

that makes code lot simpler:

try {     using (memorystream outstream = new memorystream())     {         using (gzipstream tinystream = new gzipstream(outstream,compressionmode.compress))         {             using (memorystream memstream = new memorystream(encoding.utf8.getbytes(s)))             {                 await memstream.copytoasync(tinystream);             }         }         compressed = outstream.toarray();     }     return compressed; } catch (exception ex) {     throw new stringcompressionexception("compressing string failed; see inner exception details.", ex); } 

also keep in mind exception message strings not localize easily, they're not should displaying end user anyway. they're debugging aid, "see inner exception details" works fine. maintenance programmers know that.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -