What is Exception Handling?

Hi,I want to introduce Exception handling.
Exceptions are nothing but,runtime errors.to catch the Exceptions at runtime developers had introduced this concept called Exception handling.Exception handling in .net is done using Try .. Catch.
Syntax for this is,
C# Code


try
{
//Problematic statements.
}
catch(ExceptionName objectname)
{
//User friendly error message;
}
finally // is Optional
{
//Always Runs
}

VB Code

Try
'Problematic statements.
Catch ex as Exception
'User friendly error message;
Finally 'Is Optional
'Allways Runs
End Try


Comments

Author: Nisar17 Apr 2009 Member Level: Gold   Points : 1

this is just syntax always try to convey your message by sample code, hope you don't mind

Author: Parvathi S02 May 2009 Member Level: Silver   Points : 2

suppose you have two numbers a and b
value of a is 10 and value of b is 0
you want to divide a and b and store the value in c
try
{
c=a/b
}
catch(DividebyzeroException e)
{
//some messages you want to give when the divide by zero error occur
}
finally
{
}
finally block gets executed irrespective of whether the exception occured or not.

Exception is the base class for all the exceptions.

Author: Parvathi S03 May 2009 Member Level: Silver   Points : 2

Ways of Handling Exception
1. try
{
}
finally
{
}
2. try
{
}
catch
{
}
3. try
{
}
catch(Exception e)
{
}
finally
{
}

try should be followed by a catch block or finally block.

finally block and catch block cannot come without try block.

Author: Siva Prasad23 Mar 2012 Member Level: Gold   Points : 1

If one catch block executed inside, then directly it will go to finally block. No other catch blocks be executed.In otherwords we can say like this as only the first matching catch block is executed among multiple catch blocks.Generic catch block should be last as it catches all exceptions.

Author: ketan Italiya30 Aug 2013 Member Level: Gold   Points : 6

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally and throw.

try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.

throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

Author: Phagu Mahato12 Sep 2013 Member Level: Gold   Points : 6

Exceptions are error condition that arise whn the normal flow of a code that is a series of method call on the stack .It is imperative to understand . Exceptions handling consists of only for keywords;
[1] Try
[2] Catch
[3]Throw
[4] Finally

Program code that explain throws an exception: C#

using System;

class example
{
static void Main()
{
try
{
int value = 1 / int.Parse("0");
Console.WriteLine(value);
}
catch (Exception examl)
{
Console.WriteLine(examl.Message);
}
}

Another code snippet for exception like this;

using System;

class Example
{
static void Main()
{
try
{
int value = 1 / int.Parse("0");
}
catch (Exception exa)
{
Console.WriteLine("HelpLink = {0}", exa.HelpLink);
Console.WriteLine("Message = {0}", exa.Message);
Console.WriteLine("Source = {0}", exa.Source);

}
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: