EXCEPTION HANDLING in C#

Posted on Updated on

EXCEPTION HANDLING

An exemption is an issue that emerges amid the execution of a system. A C# exemption is a reaction to an extraordinary condition that emerges while a project is running, for example, an endeavor to separate by zero.

Special cases give an approach to exchange control starting with one part of a system then onto the next. C# special
case taking care of is based upon four catchphrases: attempt, get, at long last, and toss.

Try: An attempt piece recognizes a square of code for which specific exemptions is actuated. It is trailed by one or more catch pieces.

Catch: A system gets an exemption with a special case handler at the spot in a project where you need to handle the issue. The catch watchword demonstrates the getting of an exemption.

Finally: The at long last square is utilized to execute a given arrangement of proclamations, whether an exemption is tossed or not tossed. For instance, in the event that you open a record, it must be shut whether a special case is raised or not.

Throw: A system tosses an exemption when an issue appears. This is done utilizing a toss catchphrase. Expecting a piece raises a special case, a system gets an exemption utilizing a blend of the attempt and get catchphrases. An attempt/get square is put around the code that may create an exemption.

Try

{

// statements causing exception

}

catch( ExceptionName e1 )

{

// error handling code

}

catch( ExceptionName e2 )

{

// error handling code

}

catch( ExceptionName eN )

{

// error handling code

}

finally

{

// statements to be executed

}

Exception Classes in C#

C# special cases are spoken to by classes. The special case classes in C# are for the most part specifically or by implication got from the System.Exception class. A percentage of the exemption classes got from the System.Exception class are the System.ApplicationException and System.SystemException classes.

The System.ApplicationException class bolsters special cases produced by application programs. Henceforth the special cases characterized by the software engineers ought to get from this class. The System.SystemException class is the base class for all predefined framework special case.

Handling Exceptions

C# gives an organized answer for the special case taking care of as attempt and get pieces. Utilizing these hinders the center system articulations are isolated from the mistake taking care of explanations.

These blunder taking care of pieces are actualized utilizing the attempt, get, lastly catchphrases.using System;

namespace ErrorHandlingApplication

{

class DivNumbers

{

int result;

DivNumbers()

{

result = 0;

}

public void division(int num1, int num2)

{

try

{

result = num1 / num2;

}

catch (DivideByZeroException e)

{

Console.WriteLine(“Exception caught: {0}”, e);

}

finally

{

.WriteLine(“Result: {0}”, result);

}

}

static void Main(string[] args)

{

DivNumbers d = new DivNumbers();

d.division(25, 0);

Console.ReadKey();

}

}

}

Creating User-Defined Exceptions

You can likewise characterize your own exemption. Client characterized special case classes are gotten from the Exception. You can likewise characterize your own exemption. Client characterized special case classes are gotten from the Exception.

Catch(Exception e)

{

Throw e

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s