Exception handling 

Screenshot_80  

Try

{

Int s = int Parse (t1. text)

S = S + 1000;

MBS (S + “”);

}

  1. Compile Time Problems are called errors.

EX: - Syntax errors  

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

 Interested in mastering .NET? Learn more about ".NET Training" in this blog post.

  1. While Typecasting
  2. While working with Files
  3. While working with Databases
  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 the try block.

  1. The syntax for a try, catch, finally

  Try{   --- --- --- -- }

Catch  (Type of exception obj)

{ }

Catch (Type of exception obj)

{ }

Finally

{ }

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

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

     Screenshot_81

   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

{

Try

{

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]);

}

//try 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 create 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 metadata 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 of Directory class:- 

  • Place a combo box and two List Boxes on the 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 ();

Try

{

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) ;

}