示例
using System;
public class Employee
{
//Create employee level property.
public int Emlevel
{
get
{
return(emlevel);
}
set
{
emlevel = value;
}
}
int emlevel;
}
public class Ex13
{
public static void PromoteEmployee(Object emp)
{
//Cast object to Employee.
Employee e = (Employee) emp;
// Increment employee level.
e.Emlevel = e.Emlevel + 1;
}
public static void Main()
{
try
{
Object o = new Employee();
DateTime newyears = new DateTime(2001, 1, 1);
//Promote the new employee.
PromoteEmployee(o);
//Promote DateTime; results in InvalidCastException as newyears is not an employee instance.
PromoteEmployee(newyears);
}
catch (InvalidCastException e)
{
Console.WriteLine("Error passing data to PromoteEmployee method. " + e);
}
}
}公共语言运行库捕捉 Catch 块没有捕捉的异常。根据运行库的配置,或者出现一个调试对话框,或者程序停止执行并出现一个包含异常信息的对话框。作者: guo329838627 时间: 2013-9-24 22:03
1.
try{}catch(){}
将容易出现异常的代码放到try里面, 当代码中出现异常时则会被cathch捕捉!