Visual C++ .NET/Statement/try catch

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Catch All exceptions

 
#include "stdafx.h"
using namespace System;
ref class MyDerivedException : public ApplicationException{
public:
    MyDerivedException( String ^err );
};
MyDerivedException::MyDerivedException(String ^err) : ApplicationException(err){
}

ref class MyException {
};

void main(){
    for (int i = 0; i < 4; i++){
        try{
            if (i == 1)
                throw gcnew ApplicationException("\tBase Exception");
            else if (i == 2)
                throw gcnew MyDerivedException("\tMy Derived Exception");
            else if (i == 3)
                throw gcnew MyException();
        }catch (ApplicationException ^e){
            Console::WriteLine(e->Message);
        }catch (...){
            Console::WriteLine("\tMy Exception");
        }
    }
}


Catch an Exception with a try/catch Block

 
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
void MyFunction() {
    int x,y,z;
    x = Int32::Parse(Console::ReadLine());
    y = Int32::Parse(Console::ReadLine());
    z = x/y;
    Console::WriteLine(z);
}
int main(void)
{
    try {
        MyFunction();
    }
    catch (...) {
        Console::WriteLine(
            "Exception. Unable to complete the operation.");
    }
    return 0;
}


Catch custom exception and then general exception

 
#include "stdafx.h"
using namespace System;
ref class MyException : Exception
{
   public:
    virtual property String^ Message
    {
        String^ get() override
        {
           return "You must supply a command-line argument.";
        }
    }
};
int main(array<String^>^ args)
{
     try
     {
         if (args->Length < 1)
         {
            throw gcnew MyException();
         }
         throw gcnew Exception();
     }
     catch (MyException^ e)
     {
           Console::WriteLine("MyException occurred! " + e->Message);
     }
     catch (Exception^ exception)
     {
           Console::WriteLine("Unknown exception!");
     }
}


Catch Exception

 
#include "stdafx.h"
using namespace System;
ref class X {};
ref class Y {};
void main()
{
    X ^x = gcnew X;
    try
    {
         Y ^y = (Y^)x;   
         Console::WriteLine("No Exception"); 
    }
    catch (InvalidCastException ^e)
    {
        Console::WriteLine("Invalid Cast Exception");
        Console::WriteLine(e->StackTrace);
    }
}


Catch IndexOutOfRangeException

 
#include "stdafx.h"
using namespace System;
int main()
{
   int i;
   array<int>^ array1 = { 0, 1, 2};
   try
   {
      i = array1[3];
   }
   catch(IndexOutOfRangeException^ e)
   {
      Console::WriteLine( "{0}, {1}" , e->ToString(), e->Message);
   }
}


Catch IO exception

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
int main()
{
   String^ filename = "textfile.txt";
   try
   {
      StreamReader^ sr2 = File::OpenText(filename);
      String^ line;
      while ((line = sr2->ReadLine()) != nullptr)
      {
         Console::WriteLine(line);
      }
   }
   catch(IOException^ e)
   {
       Console::WriteLine("Exception! {0}", e->Message );
   }
}


Multi Exception Handling

 

#include "stdafx.h"
using namespace System;
ref class LevelOneException : public ApplicationException{
public:
    LevelOneException( String ^err );
};
LevelOneException::LevelOneException(String ^err) : ApplicationException(err) 
{
}
ref class LevelTwoException : public LevelOneException{
public:
    LevelTwoException( String ^err );
};
LevelTwoException::LevelTwoException(String ^err) : LevelOneException(err)
{
}
void main(){
    for (int i = 0; i < 4; i++){
        try{
            if (i == 1)
                throw gcnew ApplicationException("\tBase Exception Thrown");
            else if (i == 2)
                throw gcnew LevelOneException("\tLevel 1 Exception Thrown");
            else if (i == 3)
                throw gcnew LevelTwoException("\tLevel 2 Exception Thrown");
        }catch (LevelTwoException ^e2){
            Console::WriteLine(e2->Message);
            Console::WriteLine("\tLevel 2 Exception");
        }
        catch (LevelOneException ^e1)
        {
            Console::WriteLine(e1->Message);
            Console::WriteLine("\tLevel 1 Exception");
        }
        catch (ApplicationException ^e)
        {
            Console::WriteLine(e->Message);
            Console::WriteLine("\tBase Exception");
        }
    }
}


Nested exception catching

 
#include "stdafx.h"
using namespace System;
int main()
{
   try
   {
       try
       {
           throw gcnew Exception("XYZ");
       }
       catch( Exception^ exception)
       {
           Console::WriteLine("Inner catch");
       }
       finally
       {
           Console::WriteLine("Inner finally");
       }
   }
   catch(Exception^ exception)
   {
        Console::WriteLine("Outer catch");
   }
   finally
   {
        Console::WriteLine("Outer finally");
   }
}