C#语言学习--基础部分(六) --异常处理
1,try catch finally,checked,unchecked,throw.注:try后面可以不跟catch语句,但后面要跟finally语句。可以同时跟catch,finally或者两者当中的一个,但不能什么都不跟。
a.WPF Demo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TryCatchStatement
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
int num1 = int.Parse(txtNum1.Text); //num1,num2的作用域在try语句块内。
int num2 = int.Parse(txtNum2.Text);
txtResult.Text = (num1 + num2).ToString();
}
catch (FormatException fExp)
{
MessageBox.Show(fExp.Message);
}
catch (OverflowException oExp)
{
MessageBox.Show(oExp.Message);
}
//catch (Exception ex) //捕获Exception异常写在最后
//{
// MessageBox.Show(ex.Message);
//}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TryCatchStatement
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
int num1 = int.Parse(txtNum1.Text); //num1,num2的作用域在try语句块内。
int num2 = int.Parse(txtNum2.Text);
txtResult.Text = (num1 + num2).ToString();
}
catch (FormatException fExp)
{
MessageBox.Show(fExp.Message);
}
catch (OverflowException oExp)
{
MessageBox.Show(oExp.Message);
}
//catch (Exception ex) //捕获Exception异常写在最后
//{
// MessageBox.Show(ex.Message);
//}
}
}
}
b.Console Demo:(checked)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CheckedUncheckedDemo
{
class Program
{
static void Main(string[] args)
{
int number = int.MaxValue;
try
{
checked
{
number++;
Console.WriteLine(number);
}
}
catch (Exception Exp)
{
Console.WriteLine(Exp.Message);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CheckedUncheckedDemo
{
class Program
{
static void Main(string[] args)
{
int number = int.MaxValue;
try
{
checked
{
number++;
Console.WriteLine(number);
}
}
catch (Exception Exp)
{
Console.WriteLine(Exp.Message);
}
}
}
}
(throw Demo)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ThrowDemo
{
class Program
{
static void Main(string[] args)
{
int month = int.Parse(Console.ReadLine());
try
{
string name = monthName(month);
}
catch (ArgumentOutOfRangeException Aexp)
{
Console.WriteLine(Aexp.Message);
}
finally {
Console.WriteLine("程序结束"); //只要finally出现在try语句中,无论是抛出异常,是否有return语句最后都会执行finally语句.
}
//if (name == "")
//{
// Console.WriteLine("您输入的月份的数值不正确");
//}
//else
//{
// Console.WriteLine(name);
//}
}
static string monthName(int month)
{
switch (month)
{
case 1:
return "一月";
case 2:
return "二月";
default:
//return "";
throw new ArgumentOutOfRangeException("您输入的月份不正确");
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ThrowDemo
{
class Program
{
static void Main(string[] args)
{
int month = int.Parse(Console.ReadLine());
try
{
string name = monthName(month);
}
catch (ArgumentOutOfRangeException Aexp)
{
Console.WriteLine(Aexp.Message);
}
finally {
Console.WriteLine("程序结束"); //只要finally出现在try语句中,无论是抛出异常,是否有return语句最后都会执行finally语句.
}
//if (name == "")
//{
// Console.WriteLine("您输入的月份的数值不正确");
//}
//else
//{
// Console.WriteLine(name);
//}
}
static string monthName(int month)
{
switch (month)
{
case 1:
return "一月";
case 2:
return "二月";
default:
//return "";
throw new ArgumentOutOfRangeException("您输入的月份不正确");
}
}
}
}
浙公网安备 33010602011771号