반응형

/**********************************************************************************************
-- Title : [2k5] TRY ~ CATCH 구문 예
-- Reference : hanbitbook.co.kr
-- Key word : try catch error_message number severity state procedure line
**********************************************************************************************/
use master;
go
drop database trycatch;
go
create database trycatch;
go
/*
-- try ... catch 예제 1
*/
use trycatch
go
create table t1 (c1 int not null primary key, c2 int check(c2 > 0));
go
insert t1 (c1, c2) values (1, 10);
go
-- 오류 정보 저장 테이블 생성
create table errorlog (
 stmtid   int identity(1,1),
 errornumber  int,
 errorseverity int,
 errorstate  int,
 errorline  smallint,
 errorprocedure varchar(1000),
 errormessage varchar(8000))
go
-- 테스트용 sp 생성
create proc usp_trycatch1
 @c1 int,
 @c2 int
as
insert t1 (c1, c2) values (@c1, @c2);
go
create proc usp_trycatch2
 @c1 int,
 @c2 int
as
set xact_abort on;
begin try
    begin transaction
        exec dbo.usp_trycatch1 @c1, @c2;
    commit transaction
end try
begin catch
    if xact_state() <> 0
        rollback transaction
 insert errorlog values
 (error_number(), error_severity(), error_state(), error_line(),
  error_procedure(), error_message());
end catch;
go
select * from errorlog;
go
exec dbo.usp_trycatch2 1, 100;
go
select * from errorlog;
go
exec dbo.usp_trycatch2 2, -1;
go
select * from errorlog;
go
/*
-- try ... catch 예제 2
*/
use adventureworks;
go
if object_id ('dbo.usp_geterrorinfo', 'p' ) is not null
    drop procedure usp_geterrorinfo;
go
-- 오류 정보를 조회할 저장 프로시저 생성
create procedure usp_geterrorinfo
as
    select
        error_number() as errornumber,
        error_severity() as errorseverity,
        error_state() as errorstate,
        error_procedure() as errorprocedure,
        error_line() as errorline,
        error_message() as errormessage;
go
begin try
    -- divide-by-zero 오류를 발생시킴
    select 1/0;
end try
begin catch
    -- 오류 정보 확인
    execute usp_geterrorinfo;
end catch;
go
/*
-- try ... catch 예제 3
*/
use adventureworks;
go
-- try...catch에서 처리할 수 없음
-- 오류가 포착되지 않고 try...catch 구문 밖으로 제어가 전달되어
-- 다음으로 높은 수준으로 이동됩니다.
begin try
    -- 존재하지 않는 테이블을 조회
    select * from nonexistenttable;
end try
begin catch
    select
        error_number() as errornumber,
        error_message() as errormessage;
end catch
go
/*
메시지 208, 수준 16, 상태 1, 줄 3
개체 이름 'nonexistenttable'이(가) 잘못되었습니다.
*/
-- sp, sp_executesql로 변경하면 catch 블록에서 오류 처리 가능
-- try 블록 내의 더 낮은 실행 수준에서 오류가 발생하는 경우에는
-- try...catch 구문에서 오류를 처리할 수 있습니다
begin try
    -- 존재하지 않는 테이블을 조회
    exec sp_executesql 'select * from nonexistenttable';
end try
begin catch
    select
        error_number() as errornumber,
        error_message() as errormessage;
end catch
go
if object_id ( n'dbo.usp_exampleproc', n'p' ) is not null
    drop procedure dbo.usp_exampleproc;
go
create procedure dbo.usp_exampleproc
as
    select * from nonexistenttable;
go
begin try
    execute dbo.usp_exampleproc
end try
begin catch
    select
        error_number() as errornumber,
        error_message() as errormessage;
end catch;
go

 

 

반응형

+ Recent posts