silly try catch question

Hi,

Stupid question I know but in my function below, where would the standard place to put the try catch be?

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            using (var db = new PublicUnitOfWork())
            {
                DisplayPageCMS(litPageTitle, litMetaKeywords, litMetaDescription, litBodyText, db, Common.CMSPages.Homepage);
            }
        }
    }

Open in new window

scm0smlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Which function(s) has the potential to throw an exception?
0
scm0smlAuthor Commented:
well displaypagecms but what if there was an error in the construction of the unit of work for some reason....?

Should it be around the unit of work?
0
BuggyCoderCommented:
well when you use using keyword the statement is automatically wrapped in try/finally block.
So Any errors will be caught and suppressed.

Ideally the statements used in using are the one's whose IDisposable implementation of Dispose is provided.

So if you want to catch exceptions and your unit of work doesn't implement Dipsose(), then its better to write the code like this:-

try
{
var db = new PublicUnitOfWork();
DisplayPageCMS(litPageTitle, litMetaKeywords, litMetaDescription, litBodyText, db, Common.CMSPages.Homepage);
}
catch(DisplayPageException ex)
{
}
catch(UnitOfWorkException ex)
{
}
catch(Exception ex)
{
}

Open in new window


Here catch blocks are placed in the order of most specific to least specific exception type.
Assuming DiplayPageException and UnitOfWorkException are custom exception.
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

scm0smlAuthor Commented:
i have a dispose function but it isnt doing anything at present...

stupid question but what should i be putting in there and how does it relate to error handling??
0
BuggyCoderCommented:
if you have any native resource to free, you can do that.
Something like a database connection or an existing transaction, a file handle etc.
Something that you want your application to take control of disposing....

It has nothing to do with error handling, its just when you use using block, if the object created inside it is implementing IDisposable, the dispose will be called so that the resources that are to be disposed can be disposed.
0
scm0smlAuthor Commented:
So this is inside the unit of work:
public class PublicUnitOfWork: IPublicUnitOfWork,IDisposable
{
    readonly VirtuTypeEntities _context;

    public PublicUnitOfWork()
    {
        _context = new VirtuTypeEntities();
    }

    public void Dispose()
    {
        
    }

Open in new window


So I want to dispose of my _context, correct?
0
BuggyCoderCommented:
it looks like you are using EF, you can do something like _context.Dispose() if it offers Dispose.
If not there is not need to derive from IDisposable.
0
scm0smlAuthor Commented:
it offers dispose yeah so it what I will do.

Thanks for your help!
0
scm0smlAuthor Commented:
Sorry one thing, if I want to handle an error that occurs within my using block.....

How would I do this....?
0
scm0smlAuthor Commented:
I'm not sure if that was actually answered or not...?
0
käµfm³d 👽Commented:
@BuggyCoder
well when you use using keyword the statement is automatically wrapped in try/finally block.
So Any errors will be caught and suppressed.
That is not correct. The error is caught, but it is not suppressed. It is bubbled up to higher layers until an equivalent catch is found or the program crashes (meaning no catch was found). A finally without a catch is NOT handling an exception.

@scm0sml
well displaypagecms but what if there was an error in the construction of the unit of work for some reason....?
I have seen differing opinions on this, but I believe MS' official recommendation is that constructors not throw exceptions. Generally speaking, your constructor should be simply setting up the state of your object, so you wouldn't really expect field assignments to raise exceptions. The discrepancy comes in, however, when you are getting values from external sources which you do not control (e.g. you read a field's value from the filesystem).
0
scm0smlAuthor Commented:
ok kaufmend so are we saying that if I want to handle an error that occur specifically within my using I have a try catch of its own?
0
käµfm³d 👽Commented:
When I say "the error is caught," I am not implying that an implicit catch is in the works. Rather I am saying that execution within the try does not continue at the line which raised the exception.
0
BuggyCoderCommented:
Don't use using block, use try/catch/finally and dispose of object in finally.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
You should handle errors at the place where it makes sense to handle errors. As I mentioned above, exceptions are "bubbled up" the stack until a compatible catch clause is found. If you were writing an ASP.NET application which queried a database for a particular user, then it might not make sense to wrap the database call itself in a try/catch--if you can't query the required data, then the user can't use the application. In this case, you might let the exception bubble up to the top layer where you redirect the user to an error page rather than give them a second attempt at displaying the page. On the other hand, if you are taking in numeric values from a user via a TextBox, and they enter in an alphabetic string, wrapping the code which does arithmetic against the TextBox's value makes sense, because you want to alert the user that he didn't enter appropriate data. In this instance, giving the user a chance to correct the data is a logical way to handle the exception.
0
scm0smlAuthor Commented:
OK thanks guys.

Been doing a bit of reading at the same time as this and I think I'll go with the try catch finally!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.