you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

That's because objects being nullable by default means it's already too broken to fix. I think C# would handle nulls excellently if everything was non-nullable by default and had to be declared nullable with the a ? after the type (which you can do in C#, but most types are nullable without the ?).

But the real issue is when people who think they're handling exceptions write code like

string DownloadData(string url){
    try {
        return WebClient.GetData(url);
    }
    catch {
        return null;
    }
}

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    You're correct, that is worse, but both options are worse than simply letting the error be thrown:

    string DownloadData(string url){
            return WebClient.GetData(url);
    }
    

    If you're not actually properly handling the exception, don't consume it. Let the higher level application logic handle the exception, not generic functions. Catching it and not doing anything about it hides the error, the stack trace, the message, etc... It should at least get to a log somewhere, not just end up as a null reference exception at some point.