PDA

View Full Version : مبتدی: سوال در مورد Exception



parsa lotfy
دوشنبه 17 اسفند 1394, 16:26 عصر
با سلام ...
اول شرمنده بابت عنوان تایپیک که خیلی کلی هستش ، واقعا نمیدونستم چی بنویسم :لبخند:

سوالی داشتم در مورد استثنا ها :

در برنامه ی تشخیص استثنای تقسیم بر صفر ، در کد زیر ویژوال استودیو خودش تشخیص میده که کدام بلوک Catch رو اجرا کنه : FormatException یا DivideByZeroException

میخواستم بدونم ویژوال استودیو از کجا میفهمه که کدام بلوک Catch باید اجرا بشه ؟!

این هم کد برنامه :

// Fig. 13.2: DivideByZeroExceptionHandling.cs
// FormatException and DivideByZeroException handlers.
using System;

class DivideByZeroExceptionHandling
{
static void Main( string[] args )
{
bool continueLoop = true; // determines whether to keep looping

do
{
// retrieve user input and calculate quotient
try
{
// Convert.ToInt32 generates FormatException
// if argument cannot be converted to an integer
Console.Write( "Enter an integer numerator: " );
int numerator = Convert.ToInt32( Console.ReadLine() );
Console.Write( "Enter an integer denominator: " );
int denominator = Convert.ToInt32( Console.ReadLine() );

// division generates DivideByZeroException
// if denominator is 0
int result = numerator / denominator;

// display result
Console.WriteLine( "\nResult: {0} / {1} = {2}",
numerator, denominator, result );
continueLoop = false;
} // end try
catch ( FormatException formatException )
{
Console.WriteLine( "\n" + formatException.Message );
Console.WriteLine(
"You must enter two integers. Please try again.\n" );
} // end catch
catch ( DivideByZeroException divideByZeroException )
{
Console.WriteLine( "\n" + divideByZeroException.Message );
Console.WriteLine(
"Zero is an invalid denominator. Please try again.\n" );
} // end catch
} while ( continueLoop ); // end do...while
} // end Main
} // end class DivideByZeroExceptionHandling


حالا فرض کنیم که چون کلاس های FormatException و DivideByZeroException ماله خود ویژوال استودیو هستش ، یه سیستمی درست کرده که متوجه بشه و هوشمند باشه مثلا !

حالا من وقتی میخوام کلاس Exception خودمو درست کنم ، مثلا NegativeNumberException ، چی کار کنم که کلاسم هوشمند باشه و موقع استفاده ، خود ویژوال استودیو از بین

چند بلوک Catch ، بلوک مورد نظر که هموون NegativeNumberException هستش رو اجرا کنه ؟؟؟؟؟

مثلا کلاس Exception زیر :

// NegativeNumberException represents exceptions caused by
// illegal operations performed on negative numbers.
using System;

class NegativeNumberException : Exception
{
// default constructor
public NegativeNumberException()
: base( "Illegal operation for a negative number" )
{
// empty body
} // end default constructor

// constructor for customizing error message
public NegativeNumberException( string messageValue )
: base( messageValue )
{
// empty body
} // end one-argument constructor

// constructor for customizing the exception's error
// message and specifying the InnerException object
public NegativeNumberException( string messageValue,
Exception inner )
: base( messageValue, inner )
{
// empty body
} // end two-argument constructor
} // end class NegativeNumberException


و برنامه ای که از این کلاس استفاده میکنه :

// Demonstrating a user-defined exception class.
using System;

class SquareRootTest
{
static void Main( string[] args )
{
bool continueLoop = true;

do
{
// catch any NegativeNumberException thrown
try
{
Console.Write(
"Enter a value to calculate the square root of: " );
double inputValue = Convert.ToDouble( Console.ReadLine() );
double result = SquareRoot( inputValue );

Console.WriteLine( "The square root of {0} is {1:F6}\n",
inputValue, result );
continueLoop = false;
} // end try
catch ( FormatException formatException )
{
Console.WriteLine( "\n" + formatException.Message );
Console.WriteLine( "Please enter a double value.\n" );
} // end catch
catch ( NegativeNumberException negativeNumberException )
{
Console.WriteLine( "\n" + negativeNumberException.Message );
Console.WriteLine( "Please enter a non-negative value.\n" );
} // end catch
} while ( continueLoop );
} // end Main

// computes square root of parameter; throws
// NegativeNumberException if parameter is negative
public static double SquareRoot( double value )
{
// if negative operand, throw NegativeNumberException
if ( value < 0 )
throw new NegativeNumberException(
"Square root of negative number not permitted" );
else
return Math.Sqrt( value ); // compute square root
} // end method SquareRoot
} // end class SquareRootTest


در واقع چه سیستمی وجود داره یا باید به وجود بیاریمش که ویژوال استودیو خودش بفهمه که کدام بلوک Catch باید استثنا به وجود امده رو گرفتار کنه ؟!؟!؟!:متفکر::متفکر::متفکر:

ببخشید طولانی شد ، با تشکر:قلب::قلب::قلب:

nunegandom
سه شنبه 18 اسفند 1394, 11:47 صبح
سلام
اگه توجه کنید به رنگها میبینید که در قسمت catch پارامتر ارسالی از نوع خاصی از کلاس هست!
:) از override ها که سر در بیارید مشکلتون حله در مورد override تحقیق کنید