You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中仅合法参数时返回类实例,代码路径未全部返回值问题求助

问题原因

你的CreateAccount方法返回类型是BankAccount,但当触发ArgumentOutOfRangeException进入catch块后,代码执行完catch内的语句就没有返回值了。C#要求所有可能的执行路径都必须返回对应类型的对象,因此会报“并非所有代码路径都返回值”的错误。

解决方案

根据你的需求,提供三种适合新手的修改方案:

方案1:返回可空类型,创建失败返回null

修改方法返回类型为BankAccount?(可空引用类型),catch块返回null,然后在Main方法里判断账户是否有效,再执行后续操作。

// main
BankAccount? account = CreateAccount();
if (account != null)
{
    MakeWithdrawl(account);
    MakeDeposit(account);
}
else
{
    Console.WriteLine("账户创建失败,无法执行存取款操作");
}

private static BankAccount? CreateAccount()
{
    Console.Write("Who is the account named under? : ");
    string name = Console.ReadLine();

    Console.Write("How much would you like to deposit? £");
    decimal amount = Convert.ToDecimal(Console.ReadLine());               

    try
    {
        var account = new BankAccount(name, amount);
        Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance}");
        
        return account;
    }
    catch (ArgumentOutOfRangeException e)
    {
        Console.WriteLine("Exception caught creating account with negative balance");
        Console.WriteLine(e.ToString());
        // 返回null标记创建失败
        return null;
    }
}

方案2:循环重试直到创建成功

如果要求必须创建账户才能继续后续流程,可以在方法内加循环,当创建失败时提示用户重新输入,直到成功创建并返回账户。

// main
BankAccount account = CreateAccount();
MakeWithdrawl(account);
MakeDeposit(account);

private static BankAccount CreateAccount()
{
    while (true)
    {
        Console.Write("Who is the account named under? : ");
        string name = Console.ReadLine();

        Console.Write("How much would you like to deposit? £");
        // 额外增加输入格式验证,避免非数字输入报错
        if (!decimal.TryParse(Console.ReadLine(), out decimal amount))
        {
            Console.WriteLine("请输入有效的数字金额");
            continue;
        }

        try
        {
            var account = new BankAccount(name, amount);
            Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance}");
            
            return account;
        }
        catch (ArgumentOutOfRangeException e)
        {
            Console.WriteLine("Exception caught creating account with negative balance");
            Console.WriteLine(e.ToString());
            Console.WriteLine("请重新输入账户信息");
            // 循环继续,让用户重新输入
        }
    }
}

方案3:将异常抛给上层处理

如果不想在CreateAccount内处理异常,可将异常抛给Main方法统一处理,让创建方法只负责核心创建逻辑。

// main
try
{
    BankAccount account = CreateAccount();
    MakeWithdrawl(account);
    MakeDeposit(account);
}
catch (ArgumentOutOfRangeException e)
{
    Console.WriteLine("账户创建失败:" + e.Message);
}

private static BankAccount CreateAccount()
{
    Console.Write("Who is the account named under? : ");
    string name = Console.ReadLine();

    Console.Write("How much would you like to deposit? £");
    decimal amount = Convert.ToDecimal(Console.ReadLine());               

    var account = new BankAccount(name, amount);
    Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance}");
    
    return account;
}

内容的提问来源于stack exchange,提问作者Lisa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:25:18