Why do Java people frequently consume exceptions silently?

前端 未结 28 1697
傲寒
傲寒 2020-11-29 17:50

I never did any serious Java coding before, but I learned the syntax, libraries, and concepts based on my existing skills (Delphi & C#). One thing I hardly understand i

28条回答
  •  心在旅途
    2020-11-29 18:02

    I find there are often 2 reasons this is done

    1. Programmer was lazy
    2. Programmer wanted to guard an entry point into there component (correctly or incorrectly)

    I do not believe this is a phenomenon limited to Java. I've seen such coding often in C# and VB.Net as well.

    On the surface it's quite shocking and looks terrible. But really it's nothing new. It occurs all the time in C++ applications which use error code return values vs. exceptions. The difference though is that ignoring a potentially fatal return value doesn't really look any different than calling a function that returns void.

    Foo* pFoo = ...;
    pFoo->SomeMethod(); // Void or swallowing errors, who knows?
    

    This code looks better but if SomeMethod() were to say return an HResult, it would be semantically no different than swallowing an exception.

提交回复
热议问题