Click here to Skip to main content
12,511,896 members (48,862 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.9K views
1 bookmarked
Posted

Try - finally alternative

, 5 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
There are times when "try"-related wrappers can be useful. For example, a Try() wrapper DLL written in a language other than C# could pass an Exception parameter to the Finally clause indicating whether the Try clause succeeded. For example:Try (() => {DoSomething();}, (Exception inner_ex)...
There are times when "try"-related wrappers can be useful. For example, a Try() wrapper DLL written in a language other than C# could pass an Exception parameter to the Finally clause indicating whether the Try clause succeeded. For example:
Try (() => {DoSomething();}, (Exception inner_ex) =>
{
    try
    {
      DoCleanup();
    }
    catch Exception ex
    {
      throw new CleanupException(outer_ex, ex);
    }
}
Note that an exception that occurs during cleanup will be made available up the calling stack, but the calling code will also be able to access the exception (if any) which prompted the cleanup in the first place.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

supercat9
Web Developer
Unknown
Embedded systems programmer since 1994.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 5 Jun 2011
Article Copyright 2011 by supercat9
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid