CREATE FUNCTION inside BEGIN TRY and END TRY
Many DDL statements must either start a batch or be the only statement in a batch.  So the general solution is to use dynamic SQL.  EG
I want to use CREATE FUNCTION in a block that will &quot;swallow&quot; errors, but this
BEGIN TRY

exec('
CREATE FUNCTION test (@ID int)
RETURNS int
AS
BEGIN
    RETURN(2 * @ID)
END
')

END TRY
BEGIN CATCH
END CATCH