Академический Документы
Профессиональный Документы
Культура Документы
Namespace
Use Braces As
Block Delimiters A Block and Its
Parent Block Sibling Blocks Can
{ Cannot Have a Have Variables with
// codes Variable with the the Same Name
Same Name {
{ int i ;
}
int i ; }
{ …
int i ; {
} int i ;
} }
The if Statement
Cascading if Statements
Syntax :
if { Boolean - expression }
first-embedded-statement
else
second-embedded-statement
int i = 0 ;
while ( i < 10 ) {
Console.WriteLine( i ) ;
i ++ ;
}
int i = 0 ;
do {
Console.WriteLine( i ) ;
i ++ ;
} while ( i < 10 );
int j = 0 ;
while ( true ) {
Console.WriteLine ( j ) ;
j ++ ;
if ( j < 10 )
continue ;
else
break ;
}
int errorCode ;
File source = new File ("code.cs") ;
if (errorCode == -1) goto Failed ;
int length = (int) source.Length ;
if (errorCode == -2) goto Failed ;
char [ ] contents = new char [ length ] ;
if (errorCode == -3) goto Failed ;
// Succeeded …
Failed: …
Exception
SystemException
OutOfMemoryException
IOException
NullReferenceException
ApplicationException
throw e;
System-Defined Exception
필요시에 , 런타임은 throw 문을 수행하여
시스템에서 정의한 예외를 발생시키고 프로그램은
즉시로 catch 문을 조사하게 된다 .
User-defined Exception
class InvalidTimeException : ApplicationException
{ … }
throw expression ;
CriticalSelection.Enter ( x ) ;
try {
...
}
finally {
CriticalSelection.Exit ( x ) ;
}
unchecked {
int number = int.MaxValue ;
Console.WriteLine (++number) ;
}
Throwing
• Avoid exceptions for normal or expected cases
• Never create and throw objects of class
Exception
• Include a description string in an Exception
object
• Throw objects of the most specific class
possible
Catching
• Arrange catch blocks from specific to general
• Do not let exceptions drop off Main