Exception handling
Try{
Int s = int Parse (t1. text)
S = S + 1000;
MBS (S + “”);
}
- Compile Time Problems are called errors.
EX: - Syntax errors
- Runtime problems are called an exception.
- An exception is a run time condition which stops the normal execution of a program abnormally.
- Generally, Exceptions will be raised
Interested in mastering .NET? Learn more about ".NET Training" in this blog post.
- While Typecasting
- While working with Files
- While working with Databases
- While managing the memory
5.To work with exception handling .net introduced 4 keywords Try, Catch, finally and throw
- When an exception is expected from a block of code, then recommended writing this logic within the try block.
- The syntax for a try, catch, finally
Try{ --- --- --- -- }
Catch (Type of exception obj)
{ }
Catch (Type of exception obj)
{ }
Finally
{ }
- Try block must be followed either with one catch, many catches, or ONE finally or ALL.
- The catch will be executed only when an exception is raised.
- Finally will be executed always irrespective of exception.
- In an exception is raised in line 2 then lines 3 and 4 will not be executed.
- To handle the exception, Micro-soft introduced – 400 predefine classes.
Exception class Hierarchy
File not formatted exception
Invalid 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
- Working with System. Io namespace is called “File handling”
- 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.
- 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) ;
}