Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

C# : C Sharp
Introduction to .NET
An Introduction To The .NET Framework
System Requirements
Application Types in C#
To Begin With C# Programming
.NET Type
Predefined Reference Types
Data types in C# and .NET
Classes and Object Model in .NET
Property in C# class
Arrays
Method Overloading
C# Tutorial Namespaces
Interfaces
Delegates in C#
Events in C#
Reflection
Exception Handling

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


Exception Handling

Previous Next



Exception Handling

Practically any program including c# .net can have some amount of errors. They can be broadly classified as compile-time errors and runtime errors. Compile-time errors are errors that can be found during compilation process of source code. Most of them are syntax errors. Runtime errors happen when program is running.

It is very difficult to find and debug the run-time errors. These errors also called exceptions. Rules of good coding style say that program must be able to handle any runtime error. Exception generates an exception call at runtime. Exceptions in C# can be called using two methods:

  • Using the throw operator. It call the manage code anyway and process an exception.
  • If using the operators goes awry, it can generate an exception.




Different Types of Exceptions

C# language uses many types of exceptions, which are defined in special classes. All of them are inherited from base class named System.Exception. There are classes that process many kinds of exceptions: out of memory exception, stack overflow exception, null reference exception, index out of range exception, invalid cast exception, arithmetic exception etc. This c# tutorial deals with DivideByZero c# exception and custom classes in c# exceptions.

C# has defined some keywords for processing exceptions. The most important are try, catch and finally.

The first one to be known is the try operator. This is used in a part of code, where there exists a possibility of exception to be thrown. But operator ?try? is always used with the operators: catch and finally.

See the following example of handling a simple exception in c#.

// try catch exception
int zero = 0;
try
{
int div = 100/zero;
}
catch(DivideByZeroException)
{
Console.WriteLine("Division by zero exception passed");
}

This code in runtime throws a DivideByZeroException and writes some message through the console. But if you want to release some resources that were created you must use try ? finally construction. Finally will be called even if there were no exceptions raised.

Bitmap bit = null;
// try finally exception
try
{
bit = new Bitmap(100,100);
}
finally
{
bit.Dispose();
Console.WriteLine("bitmap is disposed");
}

In the similar way we can use try ? catch ? finally construction.

Some larger projects might have a requirement of creating their own custom exception classes. Let us try to create class that validates email address. It will validate for the ?@? symbol. Please have a look on the following piece of code:

public class TextException : Exception
{
public TextException() : base()
{
}

public TextException(string message) : base(message)
{
}
}

public class MailValidator
{
MailValidator()
{
}

private static char symbol = '@';

public static void TestEnteredMail(string mailAddress)
{
if(mailAddress.IndexOf(symbol)==-1)
{
Console.WriteLine("The string entered is not a valid email address");
throw(new TextException());
}
}
}

Here were created a C# .Net TextException class that inherits from System.Exception class of .NET class library. Actually it does nothing, but there is an additional class MailValidator. It has TestEnteredMail method that raises a TextException. Now look at usage of it in Main function.

try
{
MailValidator.TestEnteredMail(Console.ReadLine());
}
catch(TextException)
{
Console.WriteLine("Exception was passed");
}

So, if user enters mail address without “@” symbol throws an exception.




Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords Exception Handling, java exception, oracle exception, c++ exception, c# exception, jsp exception, exception api, exception tutorial, throw exception, exception framework, exception vc++, exception plsql, struts exception, exception throwing, package handling, throws exception, exception servlet, exception sql, sample handling, exception block, catch exception, application exception, exception method


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.