#include<iostream>
#include<string.h>
using namespace std;
//eccezione personalizzata
class ex
{
private:
char *msg;
public:
ex()
{
}
ex(char* msg)
{
this->msg=new char[100];
strcpy(this->msg,msg);
}
//*metodo che ritorna il messaggio dell'eccezione
const char* what() const throw() {
return msg;
}
};
void fun()
{
int n;
int y;
try
{
cin>>n;
if(n==0)
throw new ex("Divisione per zero, lanciata dalla funzione funz e gestita nella funzione");
}catch(ex *e)
{
cout<<e->what();
}
}
void fuunz() noexcept(false)
{
int n;
cin>>n;
if(n==0)
throw new ex("Divisione per zero ri-lanciata dalla funzione fuunz e gestita nel main");
}
int main()
{
//fun gestisce da sola le eccezzioni, per cui non serve gestirle nel main
fun();
//fuunz non gestisce le eccezzione perchè le rilancia, per cui viene gestita nel main
try
{
fuunz();
}catch(ex *e)
{
cout<<e->what();
}
}
Post Views: 1.071