Using catch without arguments

前端 未结 5 1917
终归单人心
终归单人心 2020-12-04 23:12

What is the difference between:

catch
{
    MessageBox.Show(\"Error.\");
}

and:

catch (Exception ex)
{
    MessageBox.Show(         


        
5条回答
  •  自闭症患者
    2020-12-05 00:02

    As of .NET 2, if you don't tweak the configuration? Nothing.

    Before then, or with some config tweak I can't remember precisely, there was the possibility of an exception being thrown from unmanaged code which didn't get converted into an Exception-compatible object.

    Note that there's another option in between, where you specify the type but no variable:

    catch (Exception)
    {
       ...
    }
    

    Personally I'd be very wary of catching an exception without even logging it. It may be required if you're calling a boneheaded API, but it's generally best avoided.

提交回复
热议问题