Catch All exceptions : try catch « Statement « Visual C++ .NET






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");
        }
    }
} 

   
  








Related examples in the same category

1.Catch an Exception with a try/catch Block
2.Catch Exception
3.Multi Exception Handling
4.Catch IndexOutOfRangeException
5.Catch IO exception
6.Nested exception catching
7.Catch custom exception and then general exception