1. What is an exception?
An exception is an error when the program runs. It violates a system or application constraint or unexpected situation during normal operations. If you use 0 to remove a number, an exception occurs.
Ii. Try... Catch... Finally... Statement structure?
Examples:
Try
{
Statement;
}
Catch (...)
{
Statement;
}
Finally
{
Statement;
}
1. The try block contains the code that is being protected by exceptions.
2. A Catch Block is a code block for handling exceptions, also known as an exception handling program.
3. Finally blocks are the code to be executed in any case, regardless of whether an exception occurs.
3. Example:
Namespace try_catch
{
Class Program
{
Static voidmain (string [] ARGs)
{
Myclass MC = new myclass ();
Try
{
MC. ();
}
Catch (dividebyzeroexception)
{
Console. writeline ("catch clause inmain ()");
}
Finally
{
Console. writeline ("finally Clause inmain ()");
}
Console. writeline ("after try statement inmain .");
Console. writeline ("-- keep running! ");
}
}
Class myclass
{
Public void ()
{
Try
{
B ();
}
Catch (system. nullreferenceexception)
{
Console. writeline ("catch clause in ()");
}
Finally
{
Console. writeline ("finally clause in ()");
}
}
Protected void B ()
{
Int x = 10, y = 0;
Try
{
X/= y;
}
Catch (system. indexoutofrangeexception)
{
Console. writeline ("catch clause in B ()");
}
Finally
{
Console. writeline ("finally clause in B ()");
}
}
}
}
The program output result is:
Finally clause in B ()
Finally clause in ()
Catch clause inmain ()
Finally clause inmain ()
After try statement inmain.
-- Keep running.