Why would anyone do Catch (Exception e) {throw e; }?

Why would anyone do this? I do not understand. Can I remove this try-catch block without affecting the code?

try
{
    Collection<SvnLogEventArgs> svnLog = GetSVNRevisionsLog(lastRevision, currentRevision, svnUrl);

    svnInfo = PopulateOutput(svnLog, svnUrl.ToString());
}
catch (Exception e)
{
    throw e;
}
+3
source share
4 answers

Why would anyone do this?

You must not do this. The closest thing you should come would be if you wanted to add some entries, in which case you should write:

try
{
  /// Do something
}
catch (Exception e)
{
   LogException(e); // Do some logging
   throw; // Don't use throw e
}

The operator throw, when used separately, stores information call stack exceptions.

, (, ), . , /, .

+8

, , throw e ( , try)

. , , :

try
{
    Collection<SvnLogEventArgs> svnLog =
          GetSVNRevisionsLog(lastRevision, currentRevision, svnUrl);

    svnInfo = PopulateOutput(svnLog, svnUrl.ToString());
}
catch (Exception e)
{
    throw;
}
+2

This try / catch block does not make sense. You only need to catch exceptions if you plan to do something with it, but nothing is done in this case (just throwing an exception is not considered something, because it is all the same). You can safely remove it.

0
source

Yes, you should remove try catch. You have to catch a certain type of exception, also if you try to catch and then throw away, the call stack information of the original exception will be lost, and the call stack information is very important for debugging.

So, in general, do not try to catch everything.

0
source

All Articles