Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I misspoke.

Ignoring the syntax for a moment, I want scoping to behave like this pseudo-code. So that the catch and finally blocks are lexically nested within the parent try block.

  try {

    OutputStream out = ...

    ...

  // Must be at end of try block
  catch ExceptionA, Exception B { ... }

  catch Exception C { ... }

  finally { ... }
    

  } // end of try

Maybe even allow catch and finally to allow single expression in addition to blocks. Just like with if/then/else.


Right, but if constructing OutputStream throws ExceptionA, you shouldn't have it in scope; it's not something you can use.


Agreed. I'll make that more clear next time. I omitted catch's parens, which makes it less clear.

I just want 'out' visible to both catch and finally.


If constructing "out" fails but one of the catch-blocks tries to use "out", what should happen?


Python is an example of a language that works like this. When the constructor throws, 'out' remains not defined, but you can just do 'out = ...' in the catch block and define one with a fallback value. So all the code after the error handing would just see a working 'out' variable. This works due to all variables being bound to the function scope, not to the code block, in Python.


Python can do that because:

* it's not an error to have an unbound name in scope; just referencing it at runtime is an error. * there is a reasonable default; anything can be set to None

This doesn't hold in C++. If your class throws during its initialization, you can't really do much (even default initialization of members may not work). Reassignment may not be possible if it was declared const.

And there's still the issue of 'which objects successfully initialized'?


In Python, sure, but I struggle to see a way to make it make sense in C++.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: