<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
try//捕获异常
{
for (int i = 0; i <= nums.Length; i++)//遍历数组所有元素
{
Console.Write(nums[i] + " ");
}
}
catch (Exception a)//访问异常对象
{
Console.Write(a.Message);//输出异常错误
}
Console.WriteLine();
Console.ReadLine();
}
}
}</span>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 4,8,12,0,10 };
try//捕获异常
{
for (int i = 0; i < nums.Length; i++)
{
int valude = 0;
valude = 240 / nums[i];
Console.WriteLine("240/{0}={1}", nums[i], valude);
}
}
catch (Exception a)//访问异常对象
{
Console.WriteLine(a.Message);//输出异常错误
}
finally
{
Console.WriteLine("有没有异常我都会运行");
}
Console.WriteLine();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text
{
class Program
{
static void Main(string[] args)
{
string str = "string";
try
{
int returnInt;
returnInt = Program.ConvertStringToInt(str);//调用转换
Console.Write(returnInt);
}
catch (FormatException a)
{
Console.WriteLine(a.Message);
}
Console.ReadLine();
}
private static int ConvertStringToInt(string str)//定义转换函数
{
int intNum = 0;
try
{
intNum = Convert.ToInt32(str);
return intNum;
}
catch
{
throw new FormatException("转换错误");//引发异常
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text
{
class MyException : SystemException { }//声明异常
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("引发异常前我是被执行的");//引发异常前的提示
throw new MyException();
Console.WriteLine("因为已经引发异常,所以我不能被执行");
}
catch (MyException)
{
Console.WriteLine("引发异常");
}
Console.ReadLine();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有