Thursday, July 4, 2013

Explaination about try catch finally.

What is try catch in programming?

  Try {
  lines of code to execute
  }
  some error or exception occurs in the code placed inside the try block
  Then to record the exception, to do some other even on the exception we use catch block
  Catch{
  recode the error and write to the log file
 

What is try catch finally in programming?

Finally block is to do some thing which must be done at anycast which means if the try block executes successfully the code inside finally
block must execute. if the code in try has some error and thows an exception to the catch block, after the execution of the catch block
the code inside finally must execute. what ever happens code inside finally will execute at the last(final).
try{}
catch{}
finally{}

How to use try catch in C# asp.net?

    public void PrintNo(string no_string)
    {
        try
        {
            int no = Convert.ToInt32(no_string);
            Response.Write(no);
        }
        catch (Exception ex)
        {
            Response.Write("The input format was wrong");
        }
        finally
        {
            Response.Write("Thanks for your Try");
        }
    }
Test and output Case 1
PrintNo("1");
Output will be:
1
Thanks for your try
Test and output Case 2
PrintNo("1x");
Output will be:
The input format was wrong
Thanks for your try

No comments:

Post a Comment