using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo12
{
//调试
//调试模式和非调试模式
//vs允许配置生成两种应用程序:调试和发布
//WriteLine()函数 可以把文本输出到控制台
//Debug.WriteLine()/Trace.WriteLine()
//两个命令函数用法几乎相同,区别在于第一个命令仅在调试模式下运行,第二个命令还可用于发布程序
//与WriteLine不同的是唯一的字符串参数用于输出消息,而不需要使用{X}语法插入变量值,必须使用+串联运算符等方式在字符串中插入变量值
class Program
{
static void Main(string[] args)
{
int[] testArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, };
int maxVal = Maxima(testArray, out int[] maxValIndices);
Console.WriteLine($"Maximum value {maxVal} found at element indices:");
foreach(int index in maxValIndices)
{
Console.WriteLine(index);
}
Console.ReadKey();
}
static int Maxima(int[] integers,out int[] indices)
{
Debug.WriteLine("Maximum value search started:");
indices = new int[1];
int maxVal = integers[0];
indices[0]= 0;
int count = 1;
Debug.WriteLine(string.Format($"Maximum value initinaized to {maxVal},at element index 0."));
for(int i = 1; i < integers.Length; i++)
{
Debug.WriteLine(string.Format($"Now looking at element at index{i}."));
if (integers[i] > maxVal)
{
maxVal = integers[i];
count = 1;
indices = new int[1];
indices[0] = i;
Debug.WriteLine(string.Format($"New maximum found. New value is {maxVal},at" + $"element index {i}."));
}
else
{
if (integers[i] == maxVal)
{
count++;
int[] oldIndices = indices;
indices = new int[count];
oldIndices.CopyTo(indices, 0);
indices[count-1] = i;
Debug.WriteLine(string.Format($"Duplicate maximum found at element index {i}"));
}
}
}
Trace.WriteLine(string.Format($"Maximum value {maxVal} found,with{count} occurrences."));
Debug.WriteLine("Maximum value search completed");
return maxVal;
}
}
//跟踪点
//把光标放在要插入代码行,单击左侧行号侧边栏,会出现一个红色圆,选中Actions复选框,单击OK按钮
//中断模式
//单击IDE中的pause按钮,也可以手动控制中断
//暂停应用程序的执行,进入中断模式。完全停止应用程序的执行。重新启动应用程序
//断点
//遇到断点,会立即进入中断模式 / 如果布尔表达式的值为true就进入中断模式/ 遇到断点一定的次数后会进入中断模式
//进入断点的其他方式
//Debug.Assert(); 编译 / trace.assert(); 发布
//监视变量
//在中断模式下,使鼠标指向源代码中的变量名,显示该变量的信息
//错误处理
//异常是在运行期间代码中产生的错误,或者由代码调用的函数产生的错误
//try...catch...finally
//try 包含抛出异常的代码
//catch 包含抛出异常时要执行的代码
//finally 包含始终会执行的代码,如果发生异常会在catch块后执行,如果没有异常会在try块之后执行
class Program1
{
static string[] eTypes = { "none", "simple", "index","nested index","filter" };
static void Main(string[] args)
{
foreach(string eType in eTypes)
{
try
{
Console.WriteLine("Main() try block rached.");
Console.WriteLine($"ThrowException(\"{eType}\" called.)");
ThrowException(eType);
Console.WriteLine("Main() try block continues");
}catch(System.IndexOutOfRangeException e) when(eType == "filter")
{
ConsoleColor BackgroundColor = ConsoleColor.Red;
Console.WriteLine("Main() FILTERED System.IndexOutOfRangeException" + $"catch block reached. Message:\n\"{e.Message}\"");
object value = ResetColor();
}catch(System.IndexOutOfRangeException e)
{
Console.WriteLine("Main() System.IndexOutOfRangeException catch" + $"block reached. Message:\n\"{e.Message}\"");
}
catch
{
Console.WriteLine("Main() general catch block reached.");
}
finally
{
Console.WriteLine("Main() finally block reached.");
}
Console.WriteLine();
}
Console.ReadKey();
}
private static object ResetColor()
{
throw new NotImplementedException();
}
static void ThrowException(string exceptionType)
{
Console.WriteLine($"ThrowException(\"{exceptionType}\") reached.");
switch(exceptionType)
{
case "none":
Console.WriteLine("Not throwing an exception.");
break;
case "simple":
Console.WriteLine("Throwing system.Exception.");
break;
case "index":
Console.WriteLine("Throwing System.IndexOutOfRangeException.");
eTypes[5] = "error";
break;
case "nested index":
try
{
Console.WriteLine("ThrowException(\"nested index\")" + "try block reached");
Console.WriteLine("ThrowException(\"index\") called.");
ThrowException("index");
}
catch
{
Console.WriteLine("ThrowException(\"nested index\") general" + "try block reached");
throw;
}
finally
{
Console.WriteLine("ThrowException(\"nested index\") finally" + "try block reached");
}
break;
case "filter":
try
{
Console.WriteLine("ThrowException(\"filter\")" + "try block reached");
Console.WriteLine("ThrowException(\"index\") called.");
ThrowException("index");
}
catch
{
Console.WriteLine("ThrowException(\"filter\") general" + "try block reached");
throw;
}
break;
}
}
}
}
C#基础-调试和错误处理
最新推荐文章于 2024-08-09 10:24:28 发布