How to refactor try-catch with logging in abstract class?

Vladik

I have code sample:

public abstract class BaseClass{
  try
  {
    return Convert(value);
  }
  catch(Exception ex)
  {
    logger.LogError(ex);
    throw new Exeption("Can't convert to type {0}", type);
  }
}

How to refactor this one. I can't remove throw because method must return type. But new exception will hide original one.

Arcord

To solve your issue you should use the keyword "throw" alone.

    try
    {
        return Convert(value);
    }
    catch(Exception ex)
    {
        logger.LogError(ex);
        throw;
    }

In that way the same exception as the one you received will be thrown again and you will keep all the stack trace.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android Java - how to refactor out a try-catch-finally with a throw in the finally, all inside an abstract class?

From Dev

Android Java - how to refactor out a try-catch-finally with a throw in the finally, all inside an abstract class?

From Dev

How to refactor a return of the same value in both catch and try?

From Dev

How can I refactor this horrid abstract java class with multiple generics into something "better"?

From Dev

How to refactor logging statements in sibling classes?

From Dev

PHP Try Catch for Entire Class

From Dev

test class for a try catch block

From Dev

How to Try Catch in symfony

From Dev

How to Try Catch in symfony

From Dev

How to refactor inner class MouseAdapter?

From Dev

Nested abstract class in an abstract class and how to implement it

From Dev

How to know what to catch with Try/Catch Block

From Dev

How to aggregate an abstract class?

From Dev

How to try/catch all exceptions

From Dev

How to apply try and catch on a function?

From Dev

compiled class problem in java try/catch block

From Dev

C# WinForms Try Catch Inside a class

From Dev

How to call an abstract function with an abstract class

From Dev

In Scala, how to refactor codes with Option class like this?

From Dev

How to refactor in Intellij - method with parameters to class with fields

From Dev

In Scala, how to refactor codes with Option class like this?

From Dev

Deciding how to refactor a class having many methods

From Dev

How to refactor class in order to be testable without reflection

From Dev

How to refactor to reduce nesting depth with try/multiple catches (NDepend)

From Dev

How to refactor survived mutations away for try-resource and an equivalent mutant?

From Dev

How to refactor to reduce nesting depth with try/multiple catches (NDepend)

From Dev

How to extends an abstract class with generics?

From Dev

How to mock An Abstract Base Class

From Dev

How to create abstract class in MagicDraw

Related Related

  1. 1

    Android Java - how to refactor out a try-catch-finally with a throw in the finally, all inside an abstract class?

  2. 2

    Android Java - how to refactor out a try-catch-finally with a throw in the finally, all inside an abstract class?

  3. 3

    How to refactor a return of the same value in both catch and try?

  4. 4

    How can I refactor this horrid abstract java class with multiple generics into something "better"?

  5. 5

    How to refactor logging statements in sibling classes?

  6. 6

    PHP Try Catch for Entire Class

  7. 7

    test class for a try catch block

  8. 8

    How to Try Catch in symfony

  9. 9

    How to Try Catch in symfony

  10. 10

    How to refactor inner class MouseAdapter?

  11. 11

    Nested abstract class in an abstract class and how to implement it

  12. 12

    How to know what to catch with Try/Catch Block

  13. 13

    How to aggregate an abstract class?

  14. 14

    How to try/catch all exceptions

  15. 15

    How to apply try and catch on a function?

  16. 16

    compiled class problem in java try/catch block

  17. 17

    C# WinForms Try Catch Inside a class

  18. 18

    How to call an abstract function with an abstract class

  19. 19

    In Scala, how to refactor codes with Option class like this?

  20. 20

    How to refactor in Intellij - method with parameters to class with fields

  21. 21

    In Scala, how to refactor codes with Option class like this?

  22. 22

    Deciding how to refactor a class having many methods

  23. 23

    How to refactor class in order to be testable without reflection

  24. 24

    How to refactor to reduce nesting depth with try/multiple catches (NDepend)

  25. 25

    How to refactor survived mutations away for try-resource and an equivalent mutant?

  26. 26

    How to refactor to reduce nesting depth with try/multiple catches (NDepend)

  27. 27

    How to extends an abstract class with generics?

  28. 28

    How to mock An Abstract Base Class

  29. 29

    How to create abstract class in MagicDraw

HotTag

Archive