黑马程序员_学习日记25_异常
2012-06-03 22:01
351 查看
1、 公共异常类
System.ArithmeticException
在算术运算期间发生的异常
System.ArrayTypeMismatchException
当存储一个数组时,如果由于被存储的元素的实际类型与数组的实际类型不兼容而导致存储失败,就会引发此异常。
System.DivideByZeroException
在试图用零除整数值时引发
System.IndexOutOfRangeException
在试图使用小于零或超出数组界限的下标索引数组时引发
System.InvalidCastException
当从基类型或接口到派生类型的显示转换在运行时失败,就会引发此异常
System.NullReferenceException
在需要使用引用对象的场合,如果使用null引用,就会引发此异常
System.OutOfMemoryException
在分配内存的尝试失败时引发
System.OverflowException
在选中的上下文中所进行的算术运算、类型转换或转换操作导致溢出时引发的异常
2、 throw语句
throw语句用于主动引发一个异常,使用throw语句可在特定情形下,自动抛出异常。例:
class Program
{
class test
{
public int MyInt(string a, string b)
{
int int1;
int int2;
int num;
try
{
int1 = int.Parse(a);
int2 = int.Parse(b);
if (int2 == 0)
{
throw newDivideByZeroException();
}
num = int1 / int2;
return num;
}
catch (DivideByZeroExceptionde)
{
Console.WriteLine("用零整除引发异常!");
Console.WriteLine(de.Message);
return 0;
}
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入分子:");
string str1 =Console.ReadLine();
Console.WriteLine("请输入分母:");
string str2 =Console.ReadLine();
test tt = new test();
//调用test类中的MyInt方法,获取键盘输入的分子与分母相除得到的值
Console.WriteLine("分子除以分母的值:" + tt.MyInt(str1,str2));
}
catch (FormatException)
{
Console.WriteLine("请输入数值格式数据");
}
Console.ReadLine();
}
}
System.ArithmeticException
在算术运算期间发生的异常
System.ArrayTypeMismatchException
当存储一个数组时,如果由于被存储的元素的实际类型与数组的实际类型不兼容而导致存储失败,就会引发此异常。
System.DivideByZeroException
在试图用零除整数值时引发
System.IndexOutOfRangeException
在试图使用小于零或超出数组界限的下标索引数组时引发
System.InvalidCastException
当从基类型或接口到派生类型的显示转换在运行时失败,就会引发此异常
System.NullReferenceException
在需要使用引用对象的场合,如果使用null引用,就会引发此异常
System.OutOfMemoryException
在分配内存的尝试失败时引发
System.OverflowException
在选中的上下文中所进行的算术运算、类型转换或转换操作导致溢出时引发的异常
2、 throw语句
throw语句用于主动引发一个异常,使用throw语句可在特定情形下,自动抛出异常。例:
class Program
{
class test
{
public int MyInt(string a, string b)
{
int int1;
int int2;
int num;
try
{
int1 = int.Parse(a);
int2 = int.Parse(b);
if (int2 == 0)
{
throw newDivideByZeroException();
}
num = int1 / int2;
return num;
}
catch (DivideByZeroExceptionde)
{
Console.WriteLine("用零整除引发异常!");
Console.WriteLine(de.Message);
return 0;
}
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入分子:");
string str1 =Console.ReadLine();
Console.WriteLine("请输入分母:");
string str2 =Console.ReadLine();
test tt = new test();
//调用test类中的MyInt方法,获取键盘输入的分子与分母相除得到的值
Console.WriteLine("分子除以分母的值:" + tt.MyInt(str1,str2));
}
catch (FormatException)
{
Console.WriteLine("请输入数值格式数据");
}
Console.ReadLine();
}
}
相关文章推荐
- 黑马程序员_学习日记一_异常
- 黑马程序员--面向对象第二部分-内部类、异常--java学习日记6(基础知识)
- 黑马程序员Java学习日记(3)异常,String,多线程
- 黑马程序员_Java学习日记第三天-多态、内部类、异常处理
- 黑马程序员--Java学习日记之IO(异常处理与finally)
- 黑马程序员-学习日记_JAVA中异常
- 黑马程序员——学习日记之内部类 异常
- 黑马程序员_Java学习日记25_高新技术4
- 黑马程序员学习日记(5)--异常与异常处理
- 黑马程序员-学习日记(异常 知识点分析)
- 黑马程序员_学习日记二_异常二
- 黑马程序员_学习日记37_601基础加强(enums_truct_类型转换_值类型与引用类型_异常处理_函数返回值)
- 黑马程序员--Java学习日记之多线程
- 黑马程序员-我的第十篇学习日记:OC中点语法,@property和@synthesize,完整创建一个可用对象
- 黑马程序员学习日记(2)--面向对象基础
- 黑马程序员_学习日记76_801ASP.NET(ORM框架CRUD、EF ModelFirst编程方式、延迟加载机制)
- 黑马程序员学习日记 (二)java语言基础
- 黑马程序员-iOS学习日记(一)OC基本语法
- 黑马程序员-iOS学习日记(三)面向对象-方法
- 黑马程序员学习日记——java语言基础,dos命令,环境变量搭建