Re: try catch ok, what about finally?




"JCollum" <jcollum@xxxxxxxxx> wrote in message
news:1163441509.251607.233440@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

What error are you trying to trap? Is it possible the error you're trying
to catch is being caught before the trigger is fired? Try these samples.
I'm not sure exactly what you're trying to do, but TRY...CATCH doesn't catch
all errors. Post some sample code and expected results, so we can try to
pinpoint the problem.

-- This one raises an error in the trigger, catches the error and inserts
-- a row in another table
CREATE TABLE test1 (i INT)
GO

CREATE TABLE test2 (msg VARCHAR(255) NOT NULL PRIMARY KEY)
GO

CREATE TRIGGER test_trig
ON test1
AFTER INSERT
AS
BEGIN
BEGIN TRY
RAISERROR ('Error', 15, 127)
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
INSERT INTO test2(msg)
VALUES ('Message')
END CATCH
END
GO

INSERT INTO test1 (i)
VALUES (1)
GO

SELECT * FROM test2
GO

DROP TABLE test1
GO

DROP TABLE test2
GO

-- This example results in a PK constraint violation, and logs a message in
the catch block
-- The result is two messages in the test2 table
CREATE TABLE test1 (i INT)
GO
CREATE TABLE test2 (msg VARCHAR(255) NOT NULL PRIMARY KEY)
GO
CREATE TRIGGER test_trig
ON test1
AFTER INSERT
AS
BEGIN
BEGIN TRY
INSERT INTO test2(msg)
VALUES ('Message')
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
INSERT INTO test2(msg)
VALUES ('Message2')
END CATCH
END
GO
INSERT INTO test1 (i)
VALUES (1)
GO
INSERT INTO test1 (i)
VALUES (1)
GO
SELECT * FROM test2
GO
DROP TABLE test1
GO
DROP TABLE test2
GO


.



Relevant Pages

  • Re: Junit newbie
    ... > time in junit versus my actual class. ... > iteratively test de/compressing a single byte array with the values ranging ... As to what if test2 requires test1 to pass? ...
    (comp.lang.java.help)
  • Re: semBCreate, SEM_Q_FIFO
    ... The problem is definitely one of context switching and scheduling. ... The two tasks (test1 and test2) were at the same priority. ... next iterations of task1 will be so fast that task1 will finish ...
    (comp.os.vxworks)
  • Re: Changing the local admin password base on the computers OU
    ... You can put the common code of the two IF-bodies in front of them, ... 'Change the local admin pwd for OU TEST1 ... 'Change the local admin pwd for OU TEST2 ... I do not know what it means to test for a computers membership in ...
    (microsoft.public.scripting.vbscript)
  • Re: Validation Options with option List
    ... If you are talking about actual tests (e.g. test1 has several questions, ... test2 has several questions and so on) you can do this. ... > Could you not just add a simple IF test in an adjoining cell ... >> i have set up an validation list option in Excel so i can choose what ...
    (microsoft.public.excel.misc)
  • data comprised of regexs (while loop weirdness)
    ... why do I only see a yes when passing the last data entry (test3) as ... <end code snipet> ...
    (comp.lang.perl.misc)