Powershell陷阱和函数处理错误

时间:2016-11-12 08:49:11

标签: powershell error-handling

如何处理函数中的错误而不将其传播到主函数?

function main() {
    trap {
        "main caught it too!"
    }
    subroutine
}
function subroutine() {
    trap {
        "subroutine caught error"
        Break
    }
    1/0
}
main

结果:

subroutine caught error
main caught it too!
Attempted to divide by zero.
...

我希望子程序能够处理它自己的错误,我不想更改全局错误处理设置$ErrorActionPreference或依赖用户设置-ErrorAction参数。

3 个答案:

答案 0 :(得分:1)

通过 continue 语句交换 break 语句:

function main() {
     trap {
         "main caught it too!"
     }
     subroutine
}

function subroutine() {
     trap {
         "subroutine caught error"
         continue
     }
     1/0; Write-host "I was executed after the ERROR"
}
main
subroutine caught error
I was executed after the ERROR

如果这还不够,我会选择try / catch作为@ restless1987 sugested。

Windows IT Pro 提供了关于陷阱的详细描述。

答案 1 :(得分:0)

陷阱不能完全处理错误 - 它仍会通过它们。您可以使用public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.avtivity_date_time); mText1 = (TextView) findViewById(R.id.text1); mPickDate1 = (Button) findViewById(R.id.pickDate1); mPickTime1 = (Button) findViewById(R.id.pickTime1); mText2 = (TextView) findViewById(R.id.text2); mPickDate2 = (Button) findViewById(R.id.pickDate2); mPickTime2 = (Button) findViewById(R.id.pickTime2); mPickDate1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID_1); } }); mPickDate2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID_2); } }); mPickTime1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(TIME_DIALOG_ID_1); } }); mPickTime2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(TIME_DIALOG_ID_2); } }); final Calendar c = Calendar.getInstance(); mYear1 = c.get(Calendar.YEAR); mMonth1 = c.get(Calendar.MONTH); mDay1 = c.get(Calendar.DAY_OF_MONTH); mHour1 = c.get(Calendar.HOUR_OF_DAY); mMinute1 = c.get(Calendar.MINUTE); mYear2 = c.get(Calendar.YEAR); mMonth2 = c.get(Calendar.MONTH); mDay2 = c.get(Calendar.DAY_OF_MONTH); mHour2 = c.get(Calendar.HOUR_OF_DAY); mMinute2 = c.get(Calendar.MINUTE); updateDisplay(); } private void updateDisplay() { mText1.setText(String.format("시작 : %d년 %d월 %d일 %d시 %d분", mYear1, mMonth1 + 1, mDay1, mHour1, mMinute1)); mText2.setText(String.format("종료 : %d년 %d월 %d일 %d시 %d분", mYear2, mMonth2 + 1, mDay2, mHour2, mMinute2)); } private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear1 = year; mMonth1 = monthOfYear; mDay1 = dayOfMonth; updateDisplay(); } }; private DatePickerDialog.OnDateSetListener mDateSetListener2 = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear2 = year; mMonth2 = monthOfYear; mDay2 = dayOfMonth; updateDisplay(); } }; private TimePickerDialog.OnTimeSetListener mTimeSetListener1 = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour1 = hourOfDay; mMinute1 = minute; updateDisplay(); } }; private TimePickerDialog.OnTimeSetListener mTimeSetListener2 = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour2 = hourOfDay; mMinute2 = minute; updateDisplay(); } }; @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID_1: return new DatePickerDialog(this, mDateSetListener1, mYear1, mMonth1, mDay1); case TIME_DIALOG_ID_1: return new TimePickerDialog(this, mTimeSetListener1, mHour1, mMinute1, false); case DATE_DIALOG_ID_2: return new DatePickerDialog(this, mDateSetListener2, mYear2, mMonth2, mDay2); case TIME_DIALOG_ID_2: return new TimePickerDialog(this, mTimeSetListener2, mHour2, mMinute2, false); } return null; } private Long getDateInMS(String stringDateTime) throws ParseException { SimpleDateFormat simpledateformat1 = new SimpleDateFormat("yyyy MM dd hh mm"); SimpleDateFormat simpledateformat2 = new SimpleDateFormat("yyyy MM dd hh mm"); String formatdate1 = simpledateformat1.format("mYear1, mMonth1, mDay1, mHour1, mMinute1"); String formatdate2 = simpledateformat2.format("mYear12, mMonth2, mDay2, mHour2, mMinute2"); Date startdate = simpledateformat1.parse(formatdate1); Date enddate = simpledateformat2.parse(formatdate2); return null; }

try\catch

答案 2 :(得分:0)

似乎是我想要的答案:退出子程序而不会进一步传播错误是Return

function main() {
    trap {
        "main caught it too!" # Will not happen
    }
    subroutine
}
function subroutine() {
    trap {
        "subroutine caught error"
        Return
    }
    1/0
    "this will not execute"
}
main

输出:

subroutine caught error