一、存储过程使用 try......catch异常处理

 

1、TRY 块以 BEGIN TRY 语句开头,以 END TRY 语句结尾。在 BEGIN TRY 和 END TRY 语句之间可以指定一个或多个 Transact-SQL 语句。


2、CATCH 块必须紧跟 TRY 块。


3、CATCH 块以 BEGIN CATCH 语句开头,以 END CATCH 语句结尾。


4、在 Transact-SQL 中,每个 TRY 块仅与一个 CATCH 块相关联。




二、存储过程异常处理实例



--
--简单try catch示例,无法处理错误


begin try
    select * * from student;
end try
begin catch
    exec proc_error_info;
end catch
go
--



--简单try catch示例,不处理错误(不存在的表对象)


begin try
    select * from st;
end try
begin catch
    exec proc_error_info;
end catch
go
--



--异常处理,能处理存储过程(触发器)中(不存在表对象)的错误信息


if (object_id('proc_select') is not null)
    drop procedure proc_select
go
create proc proc_select
as
    select * from st;
go
begin try
    exec proc_select;
end try
begin catch   
    exec proc_error_info;
end catch
go




参考资料:  存储过程异常处理    http://www.studyofnet.com/news/527.html