13 August 2012

Try Catch Final Statement

In this article, we learn Try .. Catch and Final Statement. First , I describe Try,Catch anfd final? Try: It contains code that may cause exception , The block executes until an exception is thrown or it is completed successfully catch: it can be used without arguments 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 Execution? 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 TRY CATCH FINAL 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 Names Menustrip mnuforApplicaiton Toolstripmenus tryCatchToolStripMenu Toolstripmenus tryCatchFinalToolStripMenu Controls in frmDisplay Controls Names Button btnProcessing 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; 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 btnProceiing_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 Boxl. 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 TRY CATCH FINAL Statement.Thanks for reading this Articles .Please any feedback naraayanan1983@gmail.Com.

1 comment:

Michelle said...

Hello Lakshmi,
This is Michelle from e-icblue, I am here want to invite you to write a small article about this data export component: http://www.e-iceblue.com/Introduce/data-export-for-net-intro.html. if you do not mind, how about this topic: export data from datatable to Text, csv, sylk and dif etc here:http://www.e-iceblue.com/Knowledgebase/Spire.DataExport/Demos/Demo/Data-Export-Text/SYLK/CSV/DIF-for-C-VB.NET.html.
Actually, you also can export data from listview, databse, command to Word, excel, pdf, html, xml, ms access, rtf etc. So why not give it a try, I will help you if you have any problem in posting it. Thanks, Hvae a great day!