Group Discounts available for 3+ students and Corporate Clients

Exception handling in C# .Net






Int s = int Parse  (t1. text)

S = S + 1000;

MBS  (S + “”);



  1. Compile Time Problems are called as errors.

EX: – Syntax errors


  1. Runtime problems are called as exception.
  2. Exception is a run time condition which stops the normal execution of a program abnormally.
  3. Generally Exceptions will be raised


  1. While Typecasting
  2. While working with Files
  3. While working with Data bases
  4. While managing the memory

5.To work with exception handling .net introduced 4 keywords



Try, Catch, finally and throw


  1. When an exception is expected from a block of code, then recommended writing this logic within try block.


  1. Syntax for try, catch, finally





Catch  (Type of exception obj)



Catch (Type of exception obj)






  1. Try block must be followed either with one catch, many catches or ONE finally or ALL.


  1. Catch will be executed only when exception is raised.
  2. Finally will be executed always irrespective of exception.
  3. In an exception is raised in line 2 then line 3 and 4 will not be executed.
  4. To handle exception, Micro soft introduced – 400 predefine classes.





 Exception class Hierarchy



arrrowFile not formatted exception

arrrowInvalid   formatted exception





Example on Exception handling  where multiple catches and finally :-

  • Open WFAP
  • Place a text box and a Button
  • Code for Button1 _ click




Int  S = int . Parse (text Box1.Text)

S =s + 1000;

Message Box. Show (“Salary  is  ”  + s);

String [] X = new String []  {“C #”, “VB”};

Message Box. Show (X [0]);

Message Box. Show (X [1]);

Message Box. Show (X [2]);




Catch  (Format Exception fe)


Message Box. Show ( fe. Message);


Catch (Exception cd)


Message Box. Show (ed. Message );


Finally {

Message Box. Show (“From  finally”);



Working with File handling

  1.  Working with System. Io namespace is called “File handling”
  2. Generally File handling is used
  • To find Drives Information
  • To find Directory and files Information.
  •    To crate and delete files and directories.
  • To manipulate Files and folders.


  1. Classes of System. Io namespace


  • Directory: used to find Drives information
  • Directory info: To find the meta data of a directory or drive
  • File: used to do to operations like copy, delete, etc….
  • File Info
  • Stream Reader
  • Stream writer




Methods of directory class


Directory . Get Logical Drive  () à returns all drive names.

Directory . Get  Directories  (path) à returns all directory  names.

Directory . Get Files  (path) à returns all  files names.




Example on Directory class:-


  • Place a combo box and two List Boxes on form
  • Using System .Io;
  • Code for form 1 _ load Event


String  []  X = Directory . Get Logical Drives ();

For  (int  I = 0; I < x. length ; I ++)

Combo Box 1. Items . Add (X [1] )


à Code for combo Box 1 Selected Index changed event


List Box 1.Items . Clear ();

String  P = Combo box 1. Selected Item . To string  ();



String [] x = Directory. Get Directories (p);

For (int I = 0; I < x. length ; I ++)

List box1. Items a Add (X[i]);


Catch  (Io Exception)


Message Box. Show (I .e Message) ;


“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at, we will update the article in 24 hours.”

0 Responses on Exception handling in C# .Net"

Leave a Message

Your email address will not be published. Required fields are marked *


Please Enter Your Details and Query.
Three + 6