English 中文(简体)
例外处理审查
原标题:Exception handling review
  • 时间:2011-10-11 12:41:59
  •  标签:
  • c#
  • exception

不要问,在我的法典中是否正确处理例外情况,那么,有人会给我一些想法,谈谈我下面的法典。

 public IEnumerable<Job> GetAll() {
        try {
            return this._context.Jobs;
        } catch (SqlException ex) {
            //error so dispose of context
            this.Dispose();
            //wrap and rethrow back to caller
            throw new CentralRepositoryException("Error getting all jobs", ex);
        }

    }

this method is part of my business logic and calls the method above

    public IEnumerable<Job> GetAllJobs() {
        try {
            return this._jobsRepository.GetAll();
        } catch (CentralRepositoryException ex) {
            //logging code to go here
            //throw back to caller
            throw;
        } catch (Exception ex) {
            this._jobsRepository.Dispose();
            //logging code to go here
            //throw simple exception to caller
            throw new CentralRepositoryException("A general exception has occurred");
        }
    }

我的习俗例外

public class CentralRepositoryException : System.Exception {
    public CentralRepositoryException(string message, Exception innerException)
        : base(message, innerException) {
    }

    public CentralRepositoryException(string message){
    }
}
问题回答

这种做法存在几个问题:

  • Returning IEnumerable<T> will cause delayed execution of the code. Even if you return IEnumerable<T>, in the code use this._jobsRepository.GetAll().ToList();
  • Delayed execution causes the error handler not to be called at all since the code will be running delayed and in the client context (i.e. wherever you are using it)
  • Wrap all your IDisposable objects in a using block

因此,另一种办法是:

public IEnumerable<Job> GetAllJobs() {
    try {
        using(var jobsRepository = new JobsRepository()) // !!! Use Dependency Injection, etc
        {
              return jobsRepository .GetAll().ToList(); // !! NOTE: ToList() avoids delayed execution

        }
    } catch (CentralRepositoryException ex) {
        //logging code to go here
        //throw back to caller
        throw;
    } catch (Exception ex) {
        //logging code to go here
        //throw simple exception to caller
        throw new CentralRepositoryException("A general exception has occurred", ex); // !!!!!! INCLUDE THE ORIGINAL ERROR !!!!!!!
    }
}

你们不要再说一例,因为你放松了 trace。 我可以看到你重新使用这些物体处置这些物体,这应当放在

除非您能够真实地处理<> /em> ,否则你就不使用渔获物,因为你want ,否则会穿透电线,从而可以固定而不是隐藏。

That s not the correct use of Dispose(). If you notice you end up writing:

this.Dispose(); 
this._jobsRepository.Dispose(); 

两者都指同一物体。 为了确保你只处理一次,申报身份证明人可以要求处置的那类人可以收回。

这意味着,如果你产生一个当地变量,你在一份发言中这样做:

using(SomethingDisposable foo = new SomethingDisposable())
{
    //...
}

或权宜处置:

SomethingDisposable foo = new SomethingDisposable();
try
{
    //...
}
finally
{
    ((IDisposable)foo).Dispose();
}

如果你创立了一个领域,你也使你的班子失去意义:

class MyDisposable : IDisposable
{
    private SomethingDisposable foo = new SomethingDisposable();

    void IDisposable.Dispose()
    {
        foo.Dispose();
    }
}

如果你以这种方式对待你们的身份证,那么你的例外情况处理就会与你的处置混淆起来。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...