rethrow
重新引发以前捕获的异常
说明
示例
捕获并重新引发异常
通过调用无输入的 surf
导致 MATLAB 引发错误。捕获异常,显示错误标识符并重新引发异常。
try surf catch ME disp(['ID: ' ME.identifier]) rethrow(ME) end
ID: MATLAB:narginchk:notEnoughInputs
Error using surf (line 49)
Not enough input arguments.
比较 throw
和 rethrow
的行为
在您的工作文件夹中创建一个函数 combineArrays
。
function C = combineArrays(A,B) try C = catAlongDim1(A,B); % Line 3 catch exception throw(exception) % Line 5 end end function V = catAlongDim1(V1,V2) V = cat(1,V1,V2); % Line 10 end
调用数组大小不同的 combineArrays
函数。
A = 1:5; B = 1:4; combineArrays(A,B)
Error using combineArrays
Dimensions of matrices being concatenated are not consistent.
堆栈指向 MATLAB 引发异常的第 5 行。
在 combineArrays
的第 5 行将 throw(exception)
替换为 rethrow(exception)
,并重新调用该函数。
combineArrays(A,B)
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in combineArrays>catAlongDim1
V = cat(1,V1,V2);
Error in combineArrays
C = catAlongDim1(A,B);
rethrow
函数保留原始堆栈并指示错误位于第 3 行。
输入参数
exception
— 包含错误的原因和位置的异常
MException
对象
包含错误的原因和位置的异常,指定为标量 MException
对象。
版本历史记录
在 R2007b 中推出
另请参阅
assert
| try, catch
| error
| MException.last
| throw
| throwAsCaller
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)