How can I pass up an exception
How can I handle an expected exception?
I have code in my MVC controller that calls the following:
u.RowKey == new SimplerAES().Dec(HttpServerUtility.UrlTokenDecode(id)));
In my other SimplerAES class:
public string Dec(byte[] encrypted)
{
return encoder.GetString(Decrypt(encrypted));
}
public byte[] Decrypt(byte[] buffer)
{
try {
MemoryStream decryptStream = new MemoryStream();
using (CryptoStream cs = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
return decryptStream.ToArray();
} catch(CryptographicException e){
//... do something with it ...
return null; // I put the return null here as I got a syntax message saying
// not all code returns
}
}
Can someone please explain how I get the message th开发者_StackOverflow社区at decrypt failed all the way up to the point where I first try to get the RowKey. Do I have to put u.RowKey within a try catch as well?
Just don't catch the exception in your method, unless you really need the "do something" part. If you don't catch the exception, it will bubble up the stack until there's code which does catch it.
If you do need to do something within that method, such as logging, you can catch the exception and rethrow it within your catch block using
throw;
Note that this is preferred over
throw e;
as the latter will rewrite the stack trace. It probably won't make much difference in this particular case, but it's worth being aware of.
I suggest you ignore cryptography for the moment, and go hunting for good articles or books on exception handling in C#. This MSDN page is probably a good starting point.
Assuming that you actually need to do something at this level, simply rethrow the exception:
catch (CrypotgraphicException e)
{
// Do something
throw;
}
However, it might be simpler not to catch it at all and let it bubble up higher to your other exception handler.
If you want to continue having the exception unwind the stack don't return null
from the catch block. you need to throw
(without the exception reference) to continue unwinding with the same exception.
e.g.
try
{
// Do stuff
}
catch(CryptographicException e)
{
// Do stuff to clean up
throw;
}
If you throw e
(which has the existing exception object) you destroy some of the stack trace data which makes it less useful. If you want to add more information, wrap the existing exception in a new exception (as the InnerException
) and then throw the new exception object. That way you preserve the full exception information.
try
{
// Do stuff
}
catch(CryptographicException e)
{
// Do stuff to clean up
Exception newEx = new Exception("Some further message", e);
throw newEx;
}
Note: Don't simply create the base Exception object, you should derive a new exception class from an existing base (if a suitable Exception class is not available) and use the derived version. This makes catching much easier because you can then catch the exact type you need and not have to have your catch block figure out what do do because you are throwing overly broad exceptions.
Only ever catch an exception if there is something useful you can do with it. Simply swallowing it an returning null in your Decrypt method is not useful. Instead, let the exception bubble up until it gets to a point where it can be usefully handled..
Do I have to put u.RowKey within a try catch as well?
This would be a more appropriate place to catch the exception...
精彩评论