.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Using TRYCATCHFINAL in C#.Net

Posted By:Narayanan       Posted Date: August 08, 2012    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

In this article, we learn Try .. Catch and Final Statement. First , I describe Try,Catch anfd final? Try: It contains code that may cause exceotuib , The block executes until an exception is thrown or it is completed successfully catch: it can be used without aruguments to catch any type of exception. Final: it is useful for cleaning up any resources allocated in the try block.
 

Where to use throw statement?
  throw statement can be used in the Catch Block.
what is mean by throw?
  used to signal the occurrence of an exception during the Program execution.
which class is using for Thrown Exception?
System.Exception.
Now , we go  to Project section.
First, we learn Try catch Block statement.
Project Description:
    Loading text file in the Application
Create a Project in the C#.net.
Project Name is Sample Application for TRYCATCHFINAL Statement.
In this Project , We have 2 Forms ,2 Class Files.
In that 2 Forms,
One Form name is     frmtrycatchfinal (It creates when Developer creates a Project)
Another Form Name is frmDisplay.
One Class file :Program.cs (it creates when Developer creates a Project.)
Another class file:FileProcessing.cs.
Controls in frmtrycatchfinal
Controls                    Names
1)Menustrip           mnuforApplicaiton.
2)Toolstripmenus    tryCatchToolStripMenu
3)Toolstripmenus    tryCatchFinalToolStripMenu
Controls in frmDisplay
Controls                    Names
1)Button              btnProcessing.
2)RichTextBox       RichTextBox1
Coding in FileProcessing.cs

Namespace:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
       public static  void readingFileusingTryCatch(string filePath,System.Windows.Forms.RichTextBox rtb)
        {
            try
            {
                string text = File.ReadAllText(filePath);
                rtb.Text = text;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        public static void readingFileusingTryCatchFinal(string filePath, System.Windows.Forms.RichTextBox rtb)
        {
            try
            {
                string text = File.ReadAllText(filePath);
                rtb.Text = text;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                MessageBox.Show("File Closed");
            }
      }
Coding in frmtrycatchfinal.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
Object Declration
public static string frmName;  
private void tryCatchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmName = Convert.ToString(frmNames.TryCatchExample);
            frmDispaly frm = new frmDispaly();
            frm.Show();
        }
      private void tryCatchFinalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmName = Convert.ToString(frmNames.TryCatchFinalExample);
            frmDispaly frm = new frmDispaly();
            frm.Show();
        }

public enum frmNames
        {
            TryCatchExample,
            TryCatchFinalExample
        }

Coding in frmDisplay.cs
private void frmDispaly_Load(object sender, EventArgs e)
        {
            this.Text = frmtrycatchfinal.frmName;
        }

        private void btnProcessing_Click(object sender, EventArgs e)
        {
            if (this.Text == Convert.ToString(frmtrycatchfinal.frmNames.TryCatchExample))
            {
                string str = @"C:\text.txt";
                FileProcessing.readingFileusingTryCatch(str, richTextBox1);
            }
              else if (this.Text == Convert.ToString(frmtrycatchfinal.frmNames.TryCatchFinalExample))
            {
                string str = @"C:\text.txt";
                FileProcessing.readingFileusingTryCatchFinal(str, richTextBox1);
            }

        }
FileProcessing.readingFileusingTryCatch :
                             It just throws the exception.
In our Project : we see the MessageBox Content like this (Could not find the "C:\text.txt");
FileProcessing.readingFileusingTryCatchFinal:
                             It just throws the exception and also release the resources in the Final Block.
In our Project : MessageBox will show like this (Could not find the "C:\text.txt") and also  MessageBox will Show like this ("File Closed")

   Conclusion:
 I think, you have an Idea about TRYCATCHFINAL Statement.Thanks for reading this Articles .Please any feedback naraayanan1983@gmail.com.


 Subscribe to Articles

     

Further Readings:

    Responses

    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend