我的代码类似于以下内容:
try{
func1();
}
catch(Exception e){
/Do something
}
static func1(){
func2();
}
static func2(){
//Exception thrown here
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
}
Run Code Online (Sandbox Code Playgroud)
当func2()中的代码行抛出异常时,我在catch子句中没有得到通知.我没有明确地抛出任何东西,我只是有静态的常规函数声明 - 没有"throw"出现在任何地方.
为什么异常不会向上传播到catch语句?
不,代码很好.您的真实代码中有一些内容您没有向我们展示.该异常传播良好:
using System;
static class Program {
static void Main() {
try{
func1();
} catch(Exception e) {
// works fine: FileNotFoundException
Console.WriteLine(e);
}
}
static void func1(){
func2();
}
static void func2() {
string filePath = "doesnot.exist";
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
}
}
Run Code Online (Sandbox Code Playgroud)
候选人:
try都是可疑的 - 高音检查它| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |