Skip to content

Instantly share code, notes, and snippets.

@Mach12
Created September 22, 2015 16:59
Show Gist options
  • Save Mach12/7dec104a432638d56fa0 to your computer and use it in GitHub Desktop.
Save Mach12/7dec104a432638d56fa0 to your computer and use it in GitHub Desktop.
Exemple d'utilisation des exceptions (C++ et C#.Net)
// This code is available for testing at http://cpp.sh/546hl
#include <string>
#include <iostream>
void ThatOneFunction()
{
// Too lazy to make it do something else, let's throw the damn exception
throw std::string("POOTIS");
}
int main(int argc, char* av[])
{
try
{
std::cout << "Trying to run doubtful code..." << std::endl;
ThatOneFunction();
}
catch(std::string datString)
{
std::cout << "Caught string exception: \"" << datString << '"' << std::endl;
}
catch(...)
{
std::cout << "The try block threw something unexpected." << std::endl;
}
return EXIT_SUCCESS;
}
// If you really wanna test this code trry http://csharppad.com/
// I don't use this myself but it sounds legit enough
using System;
namespace NamespaceNoOneCaresAbout
{
public class DerpClass
{
public DerpClass(int somethingThatMustBePositive)
{
if (somethingThatMustBePositive < 0)
throw new ArgumentException("I want a positive number, dummy!");
}
}
public class Program
{
public void Main(string[] args)
{
try
{
new DerpClass(-1);
}
catch(ArgumentException ex)
{
Console.WriteLine("Caught ArgumentException saying: {0}", ex.Message);
}
catch(Exception ex)
{
Console.WriteLine("Well, crud.");
}
finally
{
// This block has no purpose here but... it exists.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment