string str, Exception inner): base(str, inner) { } protected RangeArrayException(
System.Runtime.Serialization.SerializationInf si,
System.Runtime.Serialization.StreamingContext sc): base(si, sc) { }
// ToStringO RangeArrayException. public override string ToStringO { return Message;
}
}
// RangeArray. class RangeArray {
// .
int[] a; // int lowerBound; // int upperBound; //
// Length, public int Length { get; private set; }
// public RangeArray(int low, int high) { high++;
if(high <= low) {
throw new RangeArrayException(" .");
}
= new int[high - low];
Length = high - low;
lowerBound = low; upperBound = high;
}
// RangeArray. public int this[int index] {
// get. get {
if(ok(index)) {
return a[index - lowerBound];
} else {
throw new RangeArrayException(" .");
}
}
// set. set {
if(ok(index)) {
a[index - lowerBound] = value;
}
else throw new RangeArrayException(" ."); }
}
// true, // , private bool ok(int index) {
if(index >= lowerBound & index <= upperBound) return true; return false;
}
}
// // , class RangeArrayDemo { static void Main() { try {
RangeArray ra = new RangeArray(-5, 5);
RangeArray ra2 = new RangeArray(1, 10);
i
// .
Console.WriteLine(" : - + .Length); for(int i = -5; i <= 5; i++) ra[i] = i;
Console.Write(" ra: "); for (int i = -5; i <= 5; i++)
Console.Write(ra[i] + " ");
Console.WriteLine("\n");
// ra2 .
Console.WriteLine(" 2: " + ra2.Length); for (int i = 1; i <= 10; i++) ra2[i] = i;
Console.Write(" ra2: "); for (int i = 1; i <= 10; i++)
Console.Write(ra2[i] + " ");
Console.WriteLine("\n");
} catch (RangeArrayException exc) {
Console.WriteLine(exc);
}
// .
Console.WriteLine(" .");
// , try {
RangeArray = new RangeArray(100, -10); // !
} catch (RangeArrayException exc) {
Console.WriteLine(exc);
}
// , try {
RangeArray = new RangeArray(-2, 2);
for(int i = -2; i <= 2; i++) ra3[i] = i;
Console.Write(" : ");
|
|
for (int i = -2; i <= 10; i++) // Console.Write([i] + " ");
} catch (RangeArrayException exc) {
Console.WriteLine(exc);
}
}
}
.
: 11
: -5-4-3-2-1012345 2: 10
2: 12345678910
.
.
: -2-1012 .
RangeArray, RangeArrayException. RangeArray : get , set RangeArray. , RangeArray try, . , RangeArray C# , , .
, RangeArrayException - , Exception, base. , , , Exception. - , Exception.
, . , ToString () . , , , , , #.
, , , catch, . , Exception , Exception . , catch . , .
, catch, . , . , : catch, , .
|
|
: ExceptA ExceptB. ExceptA Exception, ExceptB ExceptA. . , , . , Exception.
// // .
Using System;
// , class ExceptA: Exception {
public ExceptA(string str): base(str) { }
public override string ToStringO { return Message;
}
}
// , ExceptA. class ExceptB: ExceptA {
public ExceptB(string str): base(str) { }
public override string ToStringO { return Message;
}
}
class OrderMatters { static void Main() {
for(int x = 0; x < 3; x++) {
try {
if (x==0) throw new ExceptA(" ExceptA"); else if(x==l) throw new ExceptB(" ExceptB");