none
Try/Catch structure RRS feed

  • Question

  • I'm 90% sure about this but all I see is "do this" vs "never do THIS" articles

    Method 1
    I think this works but, is generally discouraged:

    try
    {
    }
     catch
    {
        try
        {
        }
        catch
        {
        }
    }

    Method 2
    I think this is terrible bad not good code:

    try

    {
       try
       {
       }
       catch
       {
       }

    }

    catch

    {

    }




    • Edited by Fred B Monday, January 13, 2020 1:57 PM
    Monday, January 13, 2020 1:56 PM

Answers

  • Method 1 can be a problem as you are in an exception and trying to catch an exception. Depending on the exception class this can fail.

    When processing a catch it is critical to do little and to avoid code that will throw an exception. YOU can get into a strange loop and end up with undefined states of the process.

    Remember that exceptions are handled by the system and not the process.

    When unwinding a serious exception the best thing to do is to just rethrow the exception to the oouter exception nandles.

    try{
        try{
        }
        catch{
            Throw $_ # defer to outer handler or create a new exception and throw it.
        }
     }
    catch{
    }

    Consider using the more traditionally acceptable open block style as it is more compact and easier to read.


    \_(ツ)_/


    • Edited by jrv Monday, January 13, 2020 2:10 PM
    • Marked as answer by Fred B Monday, January 13, 2020 2:15 PM
    Monday, January 13, 2020 2:10 PM

All replies

  • Method 1 can be a problem as you are in an exception and trying to catch an exception. Depending on the exception class this can fail.

    When processing a catch it is critical to do little and to avoid code that will throw an exception. YOU can get into a strange loop and end up with undefined states of the process.

    Remember that exceptions are handled by the system and not the process.

    When unwinding a serious exception the best thing to do is to just rethrow the exception to the oouter exception nandles.

    try{
        try{
        }
        catch{
            Throw $_ # defer to outer handler or create a new exception and throw it.
        }
     }
    catch{
    }

    Consider using the more traditionally acceptable open block style as it is more compact and easier to read.


    \_(ツ)_/


    • Edited by jrv Monday, January 13, 2020 2:10 PM
    • Marked as answer by Fred B Monday, January 13, 2020 2:15 PM
    Monday, January 13, 2020 2:10 PM
  • Thanks JRV!  Proofing someone else's code and I didn't want to point out an error that wasn't
    Monday, January 13, 2020 2:15 PM
  • You code is not a formal error but does beg more information about the usage. Using exceptions to make choices is bad for both code readability and understandability as well as making code harder to debug.  Also, exceptions are expensive so we only want to use them for the unusual circumstances.  The most common an most useful reason to use an exception handler is to prevent "run-on" code execution.  Catching the first exception skips the rest of the code preventing more exceptions.  Rethrowing the exception normally is the correct response to the caller. The calling code is asking the question or performing an operation. That code wants to know about the exception so it is the better place to handle the response to the exception since its try block will want to skip further code execution.

    Designing exception handling for a script or program is a design challenge and is also an art. There are books written about this and many technical blog articles although most are for C/C++/C#,


    \_(ツ)_/

    Monday, January 13, 2020 2:37 PM