C#基础学习之异常捕获用法小例

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  Message
{
    
class FileOperation
    
{
        
public void FileOpen(string filename)
        
{
            
throw new Exception("文件打开抛出异常");
        }


        
public void FileRead()
        

        }


        
public void FileWrite()
        

        }


        
public void FileClose()
        

        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"打开文件测试,可能抛出异常");

            
try
            
{
                FileOperation fileop 
= new FileOperation();

                fileop.FileOpen(
"/test.txt");
                fileop.FileRead();
            }

            
catch (Exception e)
            
{
                Console.WriteLine(e.Message);
            }

        }

    }

}

你可能感兴趣的:(C#基础学习之异常捕获用法小例)