Note of CLEAN CODE chapter 7 - Error Handling

本文讨论了如何通过使用异常而非返回码来改善代码逻辑的清晰度,提倡在设计中区分设备关闭操作和错误处理,以及编写测试时优先考虑try-catch-finally结构。此外,作者强调了检查性异常的局限性,提倡使用无检查异常,并提倡在处理null值和参数时提供一致的行为。

Error handling is important, but if it obscures logic, it’s wrong

Use Exceptions rather than return codes

In languages which do not support exceptions, you either set an error flag or return an error code that the caller could check like this

DeviceHandle handle = getHandle(DEV);
if(handle != DeviceHandle.INVALID){

}else{
    logger.log(errorMessage);
}

It clutters the caller. The caller must check for erros immediately after the call. But it is easy to froget, so it is better to throw an exception

public void sendShutDown(){
    try{
        tryShutDown();
    } catch(DeviceHandleException e){
        logger.log(e);
    }
}
    

private tryShutDown() throws DeviceHandleException {
    DeviceHandle handle = getHandle(DEV);
}

private DeviceHandle getHandle(DeviceId id){
    throw new DeviceHandleException(errorMessage);
}

Notice how much clearer it is. Two concerns that were tangled and now the device shut down and error handling are divided.

Write Your try-catch-finally Statement First

When you execute code in the try portion, you are stating that execution can abort at any point and the resume at the catch.

In this way, try blocks are like trasactions. Your catch has to leave your program in a consistent state, no matter what happens in the try

First thing first, we start with a unit test that shows that we’ll get an exception when the file does not exist

@Test(expected = StorageException.class)
public void testRetrive(){
    retriveSection("invalid file");
}

The test drives us to create the stub

public String retrieveSection(String sectionName){
    // dummy return until we have a real implemention
    return new String();
}

Our test fails because it does not throw an exception. Next it attempts to throw one

public String retrieveSection(String sectionName){
    try{
        FileInputStream stream = new FileInputStream(sectionName);
        stream.close();
    }catche(Exception e){
        throw new StorageException("retrive error ", e);
    }
    return new String();
}

Finally, narrow the type of exception we catch to match from FileInputStream constructor

public String retrieveSection(String sectionName){
    try{
        FileInputStream stream = new FileInputStream(sectionName);
        stream.close();
    }catche(FileNotFoundException e){
        throw new StorageException("retrive error ", e);
    }
    return new String();
}

Use Unchecked Exception

Checked excpetions (like IOExcpetion) are not necessary for the robust of productions.

Price of checked exceptions are Open/Closed Principle violation

If you throw a checked exception from a mathod and the catch is three levels above, you must declare that exception (add throws) in the signature of between you and the catch. A change in low level force signature changes on many higher levels.

This breaks the encapsulation, resulting in a cascade of changes that their way from the lowest levels of the software to the highest.

Provide Context with Exceptions

To determine the source and location of an error, you need to create informative error messages and pass them along with your exceptions.

Define Exception Classes in Terms of a Caller’s Needs

When we define exception classes in an application, our most important concern should be how they are caught.

ACEMport port = new ACMEport(12);
try{
    port.open();
} catch(DeviceResponseException e){
    reportPortError(e);
    logger.log("DeviceResponseException ", e);
} catch(GMXError e){
    reportPortError(e);
    logger.log("GMXError ", e);
} catch(OtherException e){
    reportPortError(e);
    logger.log("Others ", e);
} finally {
    ...
}

There is a lot of duplication. In most situations, the work is relatively standard regardless of the cause, record an error and make sure we can proceed

If all we do are similar regardless of the exception, we an simply fi them by wrapping the API we are calling and make sure it returns a common exception type

LocalPort port = new LocalPort(12);
try{
    port.open();
} catch(PortOpenError e){
    reportPortError(e);
    logger.log("DeviceResponseException ", e);
} finally {
    ...
}

and your localPort is a simple wrapper that catch and translates exceptions thrown by ACMPort

public class LocalPort {
    private ACMPort innerPort;
    public LocalPort(int portNumber){
        innerPort = new ACMEport(12);
    }

    public static void open(){
        try{
            port.open();
        } catch(DeviceResponseException e){
            throw new PortOpenError(e);
        } catch(GMXError e){
            throw new PortOpenError(e);
        } finally {
            ...
        }
    };
};

  • When you wrap a third-party API, you minimize your dependencies upon it
  • you are not tied to a particular vendor’s API design choices, you can define your own ones and write it more clearly

Define the Normal Flow

You define a handler above your code so that you can deal with any aborted computation. Sometimes you don’t want to abort

try{
    MealExpense expense = expenseDAO.getMeals(employee.getId());
    total += expense.getTotal();
}catch(MealExpenseNotFound e){
    total += getMealPerDiem();
}

The exception clutters the logic, it would be better if we did not have to deal with the special case

MealExpense expense = expenseDAO.getMeals(employee.getId());
total += expense.getTotal();
public class PerDiemMealExpense implements MealExpense{
    public getTotal(){
        //return meal expense per diem
    }
}

Don’t Return Null

If you work in code like this, it won’t be bad for you, but it is bad.

if(item != null){
    Registery reg = getRegister();
    if(reg != null){
        ...
    }
}

If we don’t check null, we will get a NullPointerException at a runtime, or someone catches it at a relative top level. Either of them is bad.

The problem might be “There is no null check in if statement”, but actually it has too many!

For example

List<Employee> employees = getEmployee();
if(employees != null){
    for(Employee employee : employees){
        ...
    }
}

Does it have to return a null? Why not write it as

List<Employee> employees = getEmployee();
for(Employee employee : employees){
    ...
}
public List<Employee> getEmployee(){
    if(/** no elements */){
        return Collections.emptyList();
    }
}

Don’t Pass Null

If you pass a null, you need to define a handler for InvalidArgumentException or assert, but it does not solve any problem!

//============================================================================== // WARNING!! This file is overwritten by the Block UI Styler while generating // the automation code. Any modifications to this file will be lost after // generating the code again. // // Filename: D:\001 NX Model\000 NX Custom Development\ART_NX_C\ArkTech\Modeling_Custom.cs // // This file was generated by the NX Block UI Styler // Created by: jzk24 // Version: NX 2406 // Date: 08-30-2025 (Format: mm-dd-yyyy) // Time: 12:07 (Format: hh-mm) // //============================================================================== //============================================================================== // Purpose: This TEMPLATE file contains C# source to guide you in the // construction of your Block application dialog. The generation of your // dialog file (.dlx extension) is the first step towards dialog construction // within NX. You must now create a NX Open application that // utilizes this file (.dlx). // // The information in this file provides you with the following: // // 1. Help on how to load and display your Block UI Styler dialog in NX // using APIs provided in NXOpen.BlockStyler namespace // 2. The empty callback methods (stubs) associated with your dialog items // have also been placed in this file. These empty methods have been // created simply to start you along with your coding requirements. // The method name, argument list and possible return values have already // been provided for you. //============================================================================== //------------------------------------------------------------------------------ //These imports are needed for the following template code //------------------------------------------------------------------------------ using NXOpen; using NXOpen.BlockStyler; using NXOpen.UF; using NXOpen.Utilities; using System; using static NXOpen.BodyDes.OnestepUnformBuilder; using OnestepPart = NXOpen.BodyDes.OnestepUnformBuilder.Part; // 为另一个 Part 定义别名 using Part = NXOpen.Part; //------------------------------------------------------------------------------ //Represents Block Styler application class //------------------------------------------------------------------------------ public class Modeling_Custom { //class members private static Session theSession = null; private static UI theUI = null; private string theDlxFileName; private NXOpen.BlockStyler.BlockDialog theDialog; private NXOpen.BlockStyler.Group group1;// Block type: Group private NXOpen.BlockStyler.Enumeration enum0;// Block type: Enumeration private NXOpen.BlockStyler.StringBlock string0;// Block type: String private NXOpen.BlockStyler.StringBlock string01;// Block type: String private NXOpen.BlockStyler.Group group;// Block type: Group private NXOpen.BlockStyler.StringBlock string02;// Block type: String private Part workPart; //------------------------------------------------------------------------------ //Constructor for NX Styler class //------------------------------------------------------------------------------ public Modeling_Custom() { try { theSession = Session.GetSession(); theUI = UI.GetUI(); theDlxFileName = "Modeling_Custom.dlx"; theDialog = theUI.CreateDialog(theDlxFileName); theDialog.AddApplyHandler(new NXOpen.BlockStyler.BlockDialog.Apply(apply_cb)); theDialog.AddOkHandler(new NXOpen.BlockStyler.BlockDialog.Ok(ok_cb)); theDialog.AddUpdateHandler(new NXOpen.BlockStyler.BlockDialog.Update(update_cb)); theDialog.AddInitializeHandler(new NXOpen.BlockStyler.BlockDialog.Initialize(initialize_cb)); theDialog.AddDialogShownHandler(new NXOpen.BlockStyler.BlockDialog.DialogShown(dialogShown_cb)); } catch (Exception ex) { //---- Enter your exception handling code here ----- throw ex; } } //------------------------------- DIALOG LAUNCHING --------------------------------- // // Before invoking this application one needs to open any part/empty part in NX // because of the behavior of the blocks. // // Make sure the dlx file is in one of the following locations: // 1.) From where NX session is launched // 2.) $UGII_USER_DIR/application // 3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly // recommended. This variable is set to a full directory path to a file // containing a list of root directories for all custom applications. // e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_BASE_DIR\ugii\menus\custom_dirs.dat // // You can create the dialog using one of the following way: // // 1. Journal Replay // // 1) Replay this file through Tool->Journal->Play Menu. // // 2. USER EXIT // // 1) Create the Shared Library -- Refer "Block UI Styler programmer's guide" // 2) Invoke the Shared Library through File->Execute->NX Open menu. // //------------------------------------------------------------------------------ public static void Main() { Modeling_Custom theModeling_Custom = null; try { theModeling_Custom = new Modeling_Custom(); // The following method shows the dialog immediately theModeling_Custom.Launch(); } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } finally { if (theModeling_Custom != null) theModeling_Custom.Dispose(); theModeling_Custom = null; } } //------------------------------------------------------------------------------ // This method specifies how a shared image is unloaded from memory // within NX. This method gives you the capability to unload an // internal NX Open application or user exit from NX. Specify any // one of the three constants as a return Value to determine the type // of unload to perform: // // // Immediately : unload the library as soon as the automation program has completed // Explicitly : unload the library from the "Unload Shared Image" dialog // AtTermination : unload the library when the NX session terminates // // // NOTE: A program which associates NX Open applications with the menubar // MUST NOT use this option since it will UNLOAD your NX Open application image // from the menubar. //------------------------------------------------------------------------------ public static int GetUnloadOption(string arg) { //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly); return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately); // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination); } //------------------------------------------------------------------------------ // Following method cleanup any housekeeping chores that may be needed. // This method is automatically called by NX. //------------------------------------------------------------------------------ public static void UnloadLibrary(string arg) { try { //---- Enter your code here ----- } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } } //------------------------------------------------------------------------------ //This method launches the dialog to screen //------------------------------------------------------------------------------ public NXOpen.BlockStyler.BlockDialog.DialogResponse Launch() { NXOpen.BlockStyler.BlockDialog.DialogResponse dialogResponse = NXOpen.BlockStyler.BlockDialog.DialogResponse.Invalid; try { dialogResponse = theDialog.Launch(); } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return dialogResponse; } //------------------------------------------------------------------------------ //Method Name: Dispose //------------------------------------------------------------------------------ public void Dispose() { if (theDialog != null) { theDialog.Dispose(); theDialog = null; } } //------------------------------------------------------------------------------ //---------------------Block UI Styler Callback Functions-------------------------- //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //Callback Name: initialize_cb //------------------------------------------------------------------------------ public void initialize_cb() { try { group1 = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group1"); enum0 = (NXOpen.BlockStyler.Enumeration)theDialog.TopBlock.FindBlock("enum0"); string0 = (NXOpen.BlockStyler.StringBlock)theDialog.TopBlock.FindBlock("string0"); string01 = (NXOpen.BlockStyler.StringBlock)theDialog.TopBlock.FindBlock("string01"); group = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group"); string02 = (NXOpen.BlockStyler.StringBlock)theDialog.TopBlock.FindBlock("string02"); //------------------------------------------------------------------------------ //Registration of StringBlock specific callbacks //------------------------------------------------------------------------------ //string0.SetKeystrokeCallback(new NXOpen.BlockStyler.StringBlock.KeystrokeCallback(KeystrokeCallback)); //string01.SetKeystrokeCallback(new NXOpen.BlockStyler.StringBlock.KeystrokeCallback(KeystrokeCallback)); //string02.SetKeystrokeCallback(new NXOpen.BlockStyler.StringBlock.KeystrokeCallback(KeystrokeCallback)); //------------------------------------------------------------------------------ } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } } //------------------------------------------------------------------------------ //Callback Name: dialogShown_cb //This callback is executed just before the dialog launch. Thus any Value set //here will take precedence and dialog will be launched showing that Value. //------------------------------------------------------------------------------ public void dialogShown_cb() { try { //---- Enter your callback code here ----- } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } } //------------------------------------------------------------------------------ //Callback Name: apply_cb //------------------------------------------------------------------------------ public int apply_cb() { int errorCode = 0; try { workPart = theSession.Parts.Work; if (workPart == null) { theUI.NXMessageBox.Show("错误", NXMessageBox.DialogType.Error, "没有打开的零件文件"); return errorCode; } // 1. 将enum0的值赋予"型号"属性 string modelType = enum0.ValueAsString; SetPartProperty(workPart, "型号", modelType); // 2. 将string0的值赋予"DB_PART_NO"属性 string partNo = string0.Value; if (!string.IsNullOrEmpty(partNo)) { SetPartProperty(workPart, "DB_PART_NO", partNo); } else { theUI.NXMessageBox.Show("警告", NXMessageBox.DialogType.Warning, "零件编号不能为空"); } // 3. 将string01的值赋予"DB_PART_NAME"属性 string partName = string01.Value; SetPartProperty(workPart, "DB_PART_NAME", partName); // 4. 将string02的值设置为与string0相同 string02.Value = string0.Value; theUI.NXMessageBox.Show("成功", NXMessageBox.DialogType.Information, "属性已成功更新"); } catch (Exception ex) { //---- Enter your exception handling code here ----- errorCode = 1; theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return errorCode; } private void SetPartProperty(Part workPart, string propertyName, string propertyValue) { try { // 检查部件是否为空 if (workPart == null) { theUI.NXMessageBox.Show("错误", NXMessageBox.DialogType.Error, "部件为空"); return; } // 使用 UFUN 检查部件是否可写 if (!IsPartWriteable(workPart)) { theUI.NXMessageBox.Show("错误", NXMessageBox.DialogType.Error, "部件不可写"); return; } // 使用 UFUN 设置属性 UFSession ufSession = UFSession.GetUFSession(); int result= ufSession.Attr.SetStringUserAttribute( workPart.Tag, propertyName, 0, propertyValue, UFAttr.Equals); if (result !=0) { // 处理错误 theUI.NXMessageBox.Show("属性设置警告", NXMessageBox.DialogType.Warning, $"属性操作返回代码: {result}"); } } catch (Exception ex) { // 异常处理 theUI.NXMessageBox.Show("设置属性错误", NXMessageBox.DialogType.Error, $"无法设置属性 '{propertyName}':{ex.Message}"); // 注意:这里不应该抛出 NotImplementedException } } // 添加检查部件可写状态的方法 private bool IsPartWriteable(Part part) { try { UFSession ufSession = UFSession.GetUFSession(); int writeStatus; object value = ufSession.Part.AskWriteStatus(part.Tag, out IsPartWriteable); // writeStatus 值为 0 表示部件可写 return IsPartWriteable; } catch { return false; } } //------------------------------------------------------------------------------ //Callback Name: update_cb //------------------------------------------------------------------------------ public int update_cb(NXOpen.BlockStyler.UIBlock block) { try { if (block == enum0) { string02.Value = string0.Value; } else if (block == string0) { //---------Enter your code here----------- } else if (block == string01) { //---------Enter your code here----------- } else if (block == string02) { //---------Enter your code here----------- } } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return 0; } //------------------------------------------------------------------------------ //Callback Name: ok_cb //------------------------------------------------------------------------------ public int ok_cb() { int errorCode = 0; try { errorCode = apply_cb(); if (errorCode == 0) { theDialog.Dispose(); } } catch (Exception ex) { //---- Enter your exception handling code here ----- errorCode = 1; theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return errorCode; } //------------------------------------------------------------------------------ //StringBlock specific callbacks //------------------------------------------------------------------------------ //public int KeystrokeCallback(NXOpen.BlockStyler.StringBlock string_block, string uncommitted_value) //{ //} //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //Function Name: GetBlockProperties //Returns the propertylist of the specified BlockID //------------------------------------------------------------------------------ public PropertyList GetBlockProperties(string blockID) { PropertyList plist = null; try { plist = theDialog.GetBlockProperties(blockID); } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return plist; } } 发现以下错误:CS1503参数5:无法从"方法组"转换为"bool" CS1061"UFPart"未包含"AskWriteStatus"的定义,并且找不到可接受第一个"UFPart"类型参数的可访问扩展方法"AskWriteStatus"(是否缺少using指令或程序集引用?) CS1657"IsPartWriteable"是一个"方法组",无法用作 ref或cput值 CS0428无法将方法组"IsPartWriteable"转换为非委托类型"bcol"。是否希望调用此方法? CS0168 声明了变量"writeStatus",但从未使用过 CS0168 声明了变量"ex",但从未使用过 CS0168 声明了变量"ex",但从未使用过 CS0168 声明了变量"ex",但从未使用过,请更改
09-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值