How to deal with exceptions

My technical manager insists on this exception mechanism:

try
{
    DoSth();
}
catch (OurException)
{
    throw;
}
catch (Exception ex)
{
    Util.Log(ex.Message, "1242"); // 1242 is unique to this catch block
    throw new OurException(ex);
}

1242 here is the identifier of a catch method that handles an exception other than OurException. Each catch block in the project must have a unique identifier, so that we can know where the exception occurred by looking at the logs.

For each method, we must catch OurException and throw it. If another type of exception occurs, we must register and mask it using the OurException method before resetting it.

Is this a smart approach? If there are any, what are the best alternatives?

Edit: I was told that stack trace does not produce meaningful results in release mode. Do you assume that catching and throwing general exceptions is okay?

Edit2: . , , , . .

+2
14

.

, . , , , .

+5

, , .

, ?

try
{
DoSth();
}
catch(Exception ex)
{
Util.Log(ex.Message, ex.StackTrace);
if(ex is OurException) throw ex;
else throw new OurException(ex); // use the original exception as the InnerException
}

, , - ?

@Ali A - , - , ?

EDIT:

, , ?

try
{
DoSth();
}
catch(Exception ex)
{
Util.HandleException(ex);
}

Util.HandleException:

public static void HandleException(ex)
{
Util.Log(ex); // Util.Log should log the message and stack trace of the passed exception as well as any InnerExceptions - remember, than can be several nested InnerExceptions.

// Any additional handling logic, such as exiting the application, or showing the user an error message (or redirecting in a web app)
}

, , , , .

+3

OurException . catch, , , Exception, :

try 
{
    DoSomething();
}
catch (DivideByZeroException)
{
    // handle it here, maybe rethrow it, whatever
}
// more catch blocks
catch (Exception)
{
    // oops, this is unexpected, so lets log it
}

, . , 1242 . , , . , :

    [Conditional("DEBUG")]
    public static void DebugPrintTrace()
    {
        StackTrace st = new StackTrace(true);
        StackFrame sf = st.GetFrame(1); // this gets the caller frame, not this one
        Console.WriteLine("Trace "
            + sf.GetMethod().Name + " "
            + sf.GetFileName() + ":"
            + sf.GetFileLineNumber() + "\n");
    } 
+2

: . - , , , . - , , , .

: . , , , , , .

+1

, , . , , , .

- . , .

, .

:

void a() {
  try {
    c();
  } catch(MyException1 ex) {
    throw;
  } catch(Exception ex) {
    log(ex);
    throw new MyException1(ex);
  }
}

void b() {
  try {
    a();
  } catch(MyException2 ex) {
    throw;
  } catch(Exception ex) {
    log(ex);
    throw new MyException2(ex);
  }
}

, . . , ( ).

, , , . , .

catch, . .

+1

, , , ?, , ? , , , ... .

0

, .

.

0

, . , .

- .

edit: , stacktrace , , !: -)

, , ? , . , - , , - !

0

, stacktrace - . , ?

, - , . try/catch, . , - , , , (, , ). , , , . , , .

0

, . , , .

0

:

  • ,

  • 2 catch, "catch (Exception ex)"?

0

, , , OurException, , , , , DoSth(), , . , , OurException , , , , .

0

, , , , - , . , - .

0

, , catch ex, EHAB, ; .., ..

- -

catch ex
{
    //do stuff
    throw ex;
}

.

0

Source: https://habr.com/ru/post/1709724/


All Articles