Csharp+asp.net Series of Tutorials (vi)

Source: Internet
Author: User
Tags definition exit define exception exception handling goto throw exception
Asp.net| Tutorial This tutorial is written in C # and ASP.net programming tutorials, what are the deficiencies please point out, or the ideal blog message in the old cat.

It's not written for days. Today there are fewer things, some netizens always ask, and then write something. May be more overlooked, I hope you can help me correct

Preprocessing directives: Unlike C + +, C # does not have a stand-alone preprocessor. In C #, preprocessing directives are not a separate processing step before the compiler starts compiling code, but are executed as part of lexical analysis. preprocessing directives begin with the # sign and at the beginning of the line.
#define指令用于定义符合, his scope is the entire file where the definition resides, and the symbol definition must precede all other statements, or before all real code. (For example: "Using System" is real code.) )
To cancel the definition of a symbol, use the #undef directive.
There are 4 conditional compilation directives: #if, #elif, #else, #endif, and they are used to conditionally include or exclude part of the program code. Conditional compilation directives and if statements have similar effects. You can also use logic with (&&), logic, or (| |) in conditional compilation directives. ), equal to (= =), not equal to (!=) operators.
eg
#define MF1
#define MF2
Using System;
public class Mikecat
{
public static void Main ()
{
#if (mf1&&! MF2)
Console.WriteLine ("MF1 is defined");
#elif (! MF1&&MF2)
Console.WriteLine ("MF2 is defined");
#elif (MF1&&MF2)
Console.WriteLine ("MF1 and MF2 are defined");
#else
Console.WriteLine ("MF1 and MF2 Not defined");
#endif
}
}//Run Result: MF1 and MF2 are defined
#error和 #warning directives are used to emit compilation errors and warnings.
eg
#define MF1
#define MF2
Using System;
public class Mikecat
{
public static void Main ()
{
#if MF1
#warning Welcome to the ideal of an old cat!
#endif
#if MF2
#error old cat's ideal blog error
#endif
}
}//Run Results: Test.cs (9,17): Warning CS1030: #warning: "Welcome to the ideal of the old cat!" ”
Test.cs (12,15): Error CS1029: #error: "The old cat's ideal blog error"
#line指令用于修改编译器行号及文件名
eg
Using System;
public class Mikecat
{
public static void Main ()
{
#line "Mfblog.cs"//Set the compile line number to 66 and rename the filename to Mfblog.cs
Intt I=1;
Console.WriteLine (The value of "I is {0}", i);
}
}//Run Result: Mfblog.cs (66,6): Error CS0246: Cannot find type or namespace name "Intt" (is the absence of a using directive or assembly reference?) )
Mfblog.cs (67,34): Error CS0103: Name "I" does not exist in class or namespace "Mikecat"

Exception handling: In C #, there are two situations in which an exception is thrown: the first is to use the throw statement in a program to throw an exception immediately and unconditionally. The second scenario is when a C # statement or expression fires an exception during execution, which causes the operation to end normally and thus throw an exception.
In C #, exceptions are handled by try statements. The Try statement provides a mechanism to catch exceptions thrown during a program. Try has three possible structures, namely: try-catch|try-finally|try-catch-finally
Try-catch structure: A try clause followed by one or more catch clauses. If an exception is thrown when the statement in the TRY clause is executed, the program finds the first catch clause that handles the exception and transfers control to the catch clause execution. No exception types are defined, and catch clauses that do not define exception variables are called common catch clauses. A try clause can have at most one normal catch clause, and the clause must be followed by another catch clause.
eg
Using System;
Class Mikecat
{
static void Mf1 (string s)
{
if (s==null)
Throw (new ArgumentNullException ());//Throw exception
}
static void Mf2 ()
{
Try
{
String S=null;
MF1 (s);//Call MF () method. An exception is thrown because of s=null
}
catch (ArgumentNullException ex)
{
Console.WriteLine (Exception in "Mf2 () method: {0}", ex. message);
Throw;//raised again
}
}
public static void Main ()
{
Try
{
MF2 ()//Call MF2 () method
}
catch (ArgumentNullException ex)
{
Console.WriteLine (Exception {0} in the Main () method, e.message);
}
}
Exception in the}//mf2 () method: value cannot be null.
The exception value in the Main () method cannot be null.
Try-finally structure: A try clause followed by a finally clause. Regardless of how the try clause exits (whether the normal exit, the exception is thrown, or even the execution of the Goto|break|continue|return statement exit), the control of the program is always transferred to the finally clause execution.
eg
Using System;
public class Mikecat
{
public static void Main ()
{
Try
{
Console.WriteLine ("Execute try clause");
Goto leave;//Jump to leave label
Return
}
Finally
{
Console.WriteLine ("Execute finally clause");
}
Leave
Console.WriteLine ("Execute leave tag!") ");
}
}//execute a try clause to execute the FINALLY clause
Try-catch-finally: A try clause followed by one or more catch clauses and a finally clause
eg
Using System;
Class Mikecat
{
static void MF (string s)
{
if (s==null)
Throw (new ArgumentNullException ());//Throw exception
}
public static void Main ()
{
Try
{
String S=null;
MF (s);//The MF () method is invoked, and because of S=null, an exception is thrown
}
catch (ArgumentNullException ex)
{
Console.WriteLine ("Exception occurred: {0}", ex.) message);
}
Finally
{
Console.WriteLine ("Execute finally clause");
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.