مقابله با خطاها در سي شارپ (Exception Handling in C#)
EXCEPTION يك خطاي زمان اجر است كه بدليل شرايطي غيرنرمال در برنامه ايجاد مي شود. در سي شارپ exeption كلاسي است در فضاي نام سيستم. شيء ايي از نوع exception بيانگر شرايطي است كه سبب رخ دادن خطا در كد شده است. سي شارپ از exception ها به صورتي بسيار شبيه به جاوا و سي پلاس پلاس استفاده مي نمايد.
دلايلي كه بايد در برنامه exception handling حتما صورت گيرد به شرح زير است:
- قابل صرفنظر كردن نيستند و اگر كدي اين موضوع را در نظر نگيرد با يك خطاي زمان اجرا خاتمه پيدا خواهد كرد.
- سبب مشخص شدن خطا در يك نقطه از برنامه شده و ما را به اصلاح آن سوق مي دهد.
بوسيله ي عبارات try...catch مي توان مديريت خطاها را انجام داد. كدي كه احتمال دارد خطايي در آن رخ دهد درون try قرار گرفته و سپس بوسيله ي يك يا چند قطعه ي catch مي توان آنرا مديريت كرد. و اگر از اين قطعات خطايابي استفاده نشود برنامه به صورتهاي زير متوقف خواهد شد :
class A {static void Main() {catch {}}}
TEMP.cs(3,5): error CS1003: Syntax error, 'try' expected
class A {static void Main() {finally {}}}
TEMP.cs(3,5): error CS1003: Syntax error, 'try' expected
class A {static void Main() {try {}}}
TEMP.cs(6,3): error CS1524: Expected catch or finally
بهتر است يك مثال ساده را در اين زمينه مرور كنيم:
int a, b = 0 ;
Console.WriteLine( "My program starts " ) ;
try
{
a = 10 / b;
}
catch ( Exception e )
{
Console.WriteLine ( e ) ;
}
Console.WriteLine ( "Remaining program" ) ;
The output of the program is:
My program starts
System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication4.Class1.Main(String[] args) in
d:\dont delete\consoleapplication4\class1.cs:line 51
Remaining program
برنامه شروع به اجرا مي كند. سپس وارد بلوك و يا قطعه ي try مي گردد. اگر هيچ خطايي هنگام اجراي دستورات داخل آن رخ ندهد ، برنامه به خط آخر جهش خواهد كرد و كاري به قطعات catch ندارد.
اما در اينجا در اولين try عددي بر صفر تقسيم شده است بنابراين كنترل برنامه به بلوك catch منتقل مي شود و صرفا نوع خطاي رخ داده شده نوشته و نمايش داده مي شود. سپس برنامه به كار عادي خودش ادامه مي دهد.
تعدادي از كلاس هاي exception در سي شارپ كه از كلاس System.Exception ارث برده اند به شرح زير هستند :
• Exception Class - - Cause
• SystemException - A failed run-time check;used as a base class for other.
• AccessException - Failure to access a type member, such as a method or field.
• ArgumentException - An argument to a method was invalid.
• ArgumentNullException - A null argument was passed to a method that doesn't accept it.
• ArgumentOutOfRangeException - Argument value is out of range.
• ArithmeticException - Arithmetic over - or underflow has occurred.
• ArrayTypeMismatchException - Attempt to store the wrong type of object in an array.
• BadImageFormatException - Image is in the wrong format.
• CoreException - Base class for exceptions thrown by the runtime.
• DivideByZeroException - An attempt was made to divide by zero.
• FormatException - The format of an argument is wrong.
• IndexOutOfRangeException - An array index is out of bounds.
• InvalidCastExpression - An attempt was made to cast to an invalid class.
• InvalidOperationException - A method was called at an invalid time.
• MissingMemberException - An invalid version of a DLL was accessed.
• NotFiniteNumberException - A number is not valid.
• NotSupportedException - Indicates sthat a method is not implemented by a class.
• NullReferenceException - Attempt to use an unassigned reference.
• OutOfMemoryException - Not enough memory to continue execution.
• StackOverflowException - A stack has overflown.
در كد فوق صرفا عمومي ترين نوع از اين كلاس ها كه شامل تمامي اين موارد مي شود مورد استفاده قرار گرفت يعني :
catch ( Exception e )
اگر نيازي به خطايابي دقيقتر باشد مي توان از كلاس هاي فوق براي اهداف مورد نظر استفاده نمود.
مثالي ديگر: ( در اين مثال خطايابي دقيق تر با استفاده از كلاس هاي فوق و همچنين مفهوم finally نيز گنجانده شده است )
int a, b = 0 ;
Console.WriteLine( "My program starts" ) ;
try
{
a = 10 / b;
}
catch ( InvalidOperationException e )
{
Console.WriteLine ( e ) ;
}
catch ( DivideByZeroException e)
{
Console.WriteLine ( e ) ;
}
finally
{
Console.WriteLine ( "finally" ) ;
}
Console.WriteLine ( "Remaining program" ) ;
The output here is:
My program starts
System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication4.Class1.Main(String[] args) in
d:\dont delete\consoleapplication4\class1.cs:line 51
finally
Remaining program
قسمت موجود در قطعه ي فاينالي همواره صرفنظر از قسمت هاي ديگر اجرا مي شود.
به مثال زير دقت كنيد :
int a, b = 0 ;
Console.WriteLine( "My program starts" )
try
{
a = 10 / b;
}
finally
{
Console.WriteLine ( "finally" ) ;
}
Console.WriteLine ( "Remaining program" ) ;
Here the output is
My program starts
Exception occurred: System.DivideByZeroException:
Attempted to divide by zero.at ConsoleApplication4.Class1.
Main(String[] args) in d:\dont delete\consoleapplication4
\class1.cs:line 51
finally
قسمت چاپ Remaining program اجرا نشده است.
عبارت throw :
اين عبارت سبب ايجاد يك خطا در برنامه مي شود.
مثال :
int a, b = 0 ;
Console.WriteLine( "My program starts" ) ;
try
{
a = 10 / b;
}
catch ( Exception e)
{
throw
}
finally
{
Console.WriteLine ( "finally" ) ;
}
در اين حالت قسمت فاينالي اجرا شده و برنامه بلافاصله خاتمه پيدا مي كند






پاسخ با نقل قول
Bookmarks