using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


public partial class StoredProcedures
{

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void EatException()
    {
        try
        {  
            int i = 42;
            int j = 0;
            j = i / j;
            
        }
        catch (Exception e)
        {
            SqlContext.Pipe.Send("ate exception");
            SqlContext.Pipe.Send(e.Message);
        }
    }

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void ExceptionThrower()
    {
        try
        {
            int i = 42;
            int j = 0;
            j = i / j;
        }
        catch (Exception e)
        {
            SqlContext.Pipe.Send("In exception thrower");
            SqlContext.Pipe.Send(e.Message);
            throw (e);
        }
    }

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void ExceptionCatcher()
    {
        using (SqlConnection conn = new SqlConnection("context connection=true"))
        using (SqlCommand cmd = new SqlCommand("ExceptionThrower", conn))
        {
            try
            {
                cmd.CommandType = CommandType.StoredProcedure;

                conn.Open();
                cmd.ExecuteNonQuery();
                SqlContext.Pipe.Send("Shouldn't get here");
            }
            catch (SqlException e)
            {
                SqlContext.Pipe.Send("In exception catcher");
                SqlContext.Pipe.Send(e.Number + " " + e.Message);
            }
        }
    }
};
