difference between try...catch and try....finally ? in asp.net(C#)

like when i want to catch error like 1/0 then i put code in try block and put exception object in catch block like respone.write("ERROR:"+ ex.Message) but advisors told me that it isn't a good practice to put catch always, it absorbs error without notifying ????? ehhhhh ? but it did via ex.Message , so why ?
and what does try....finally do ? i know that it is used to release resources but of what use is TRY if exception can't be catched ?

but advisors told me that it isn't a good practice to put catch always..

I'd agree with that statement if you are not able to handle the error correctly. In addition to handling the error, you need to consider how you will inform the user that there was an error, or the user may think the transaction was completed.

ex.Message back to the developer is good, but not so much for a user...

and what does try....finally do ? i know that it is used to release resources but of what use is TRY if exception can't be catched ?

You are correct... finally is where you generally release resources. this code block is going to run regardless of what happens in the try or catch section. A Finally block is always executed when execution leaves any part of the Try...Catch statement. The Finally block is always executed.

Maybe there was a miscommunication in that discussion you had with the advisors, or I'm missing something...

ok jorGem, Great but i saw many examples using

Try
{
  some code...
}

finally 
{
  object.close/dispose; // incase of db objects
}

my question is that here we are using TRY only because we want to execute finally ? and we should "always put a clean and less likely to throw exception" code in try ? in this case ? i.e. after several testing and debuggings

like what i concluded from vast study about his topic is finally this:

try/catch/finally:

try
{
    // open some file or connection
    // do some work that could cause exception
}
catch(Exception ex)
{
   // do some exception handling: rethrow with a message, log the error, etc...
   // it is not a good practice to just catch and do nothing (swallow the exception)
}
finally
{
    // do some cleanup to close file/connection
    // guaranteed to run even if an exception happened in try block
    // if there was no finally, and exception happened before cleanup in  your try block, file could stay open.
}
....................................................................................

Try/Finally:

try
{
    // open some file/connection
    // do some work, where you're not expecting an exception
    // or, you don't want to handle the exception here, rather just let it go to the caller, so no need for a catch
}
finally
{
    // do cleanup, guaranteed to go in this finally block
}

I would agree with the results of your study...a comment about your note here..

line 31 --> // do cleanup, guaranteed to go in this finally block

In a try..finally, there is no catch block to handle the exception. Therefore if you do have an exception, the unhandled exception could end the application and the code in the finally block would not execute.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.