Exception Handling Frequency/Log Detail

I am working on a fairly complex .NET application that interacts with another application. Many single-line statements are possible culprits for throwing an Exception and there is often nothing I can do to check the state before executing them to prevent these Exceptions.

The question is, based on best practices and seasoned experience, how frequently should I lace my code with try/catch blocks? I’ve listed three examples below, but I’m open to any advice.

I’m really hoping to get some pros/cons of various approaches. I can certainly come up with some of my own (greater log granularity for the O-C approach, better performance for the Monolithic approach), so I’m looking for experience over opinion.


EDIT: I should add that this application is a batch program. The only “recovery” necessary in most cases is to log the error, clean up gracefully, and quit. So this could be seen to be as much a question of log granularity as exception handling. In my mind’s eye I can imagine good reasons for both, so I’m looking for some general advice to help me find an appropriate balance.


Monolitich Approach

class Program{
    public static void Main(){
        try{
            Step1();
            Step2();
            Step3();
        } catch (Exception e) {
            Log(e);
        } finally {
            CleanUp();
        }
    }

    public static void Step1(){
        ExternalApp.Dangerous1();
        ExternalApp.Dangerous2();
    }

    public static void Step2(){
        ExternalApp.Dangerous3();
        ExternalApp.Dangerous4();
    }

    public static void Step3(){
        ExternalApp.Dangerous5();
        ExternalApp.Dangerous6();
    }
}

Delegated Approach

class Program{
    public static void Main(){
        try{
            Step1();
            Step2();
            Step3();
        } finally {
            CleanUp();
        }
    }

    public static void Step1(){
        try{
            ExternalApp.Dangerous1();
            ExternalApp.Dangerous2();
        } catch (Exception e) {
            Log(e);
            throw;
        }
    }

    public static void Step2(){
        try{
            ExternalApp.Dangerous3();
            ExternalApp.Dangerous4();
        } catch (Exception e) {
            Log(e);
            throw;
        }
    }

    public static void Step3(){
        try{
            ExternalApp.Dangerous5();
            ExternalApp.Dangerous6();
        } catch (Exception e) {
            Log(e);
            throw;
        }
    }
}

Obsessive-Compulsive Approach

class Program{
    public static void Main(){
        try{
            Step1();
            Step2();
            Step3();
        } finally {
            CleanUp();
        }
    }

    public static void Step1(){
        try{
            ExternalApp.Dangerous1();
        } catch (Exception e) {
            Log(e);
            throw;
        }
        try{
            ExternalApp.Dangerous2();
        } catch (Exception e) {
            Log(e);
            throw;
        }
    }

    public static void Step2(){
        try{
            ExternalApp.Dangerous3();
        } catch (Exception e) {
            Log(e);
            throw;
        }
        try{
            ExternalApp.Dangerous4();
        } catch (Exception e) {
            Log(e);
            throw;
        }
    }

    public static void Step3(){
        try{
            ExternalApp.Dangerous5();
        } catch (Exception e) {
            Log(e);
            throw;
        }
        try{
            ExternalApp.Dangerous6();
        } catch (Exception e) {
            Log(e);
            throw;
        }
    }
}

Other approaches welcomed and encouraged. Above are examples only.

5

Catch at the granularity that is meaningful to your application. If you are not going to take a different action for a different exception source, then there is no need to break those things apart.

3

I favor approach one.

The exceptions are going to go up the call stack to the try/catch block in the main method, and get logged there anyway.

Don’t catch in your step methods unless you can handle each exception locally somehow.

3

Ask yourself why exception syntax was invented when it’s perfectly possible to handle unexpected conditions without them. In C, all code looks like your obsessive-compulsive approach, except for instead of a try/catch block, the return value of the function indicates success or failure.

The reason exceptions were invented is to allow code to look like your monolithic approach. It’s much cleaner, follows the DRY principle, and it’s easier to have people write the lower layers without imposing strict rules on exception handling in every single function. That should be your default mode of operation, and you should only also catch exceptions at the lower layers if you can actually recover from the error, not just report it.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật