强制控制台应用程序失败

编程入门 行业动态 更新时间:2024-10-26 09:31:14
本文介绍了强制控制台应用程序失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直对错误处理,何时抛出等的正确方法感到困惑.我有一个控制台应用程序,它会检查文件夹中的文件列表,如果在特定的时间内没有显示所有文件,我想强制该应用程序 失败.该应用程序处于长时间运行的SQL Agent作业中,该作业在深夜运行.迫使应用程序出错的最佳实践是什么,因为如果所有文件都不存在,则不想处理其余的作业步骤.

I have always been confused the correct way for error handling, when to throw and etc. I have a console application that checks for a list of files in a folder, if all the files are not present in a particular length of time I want to force the application to fail. This app is in long SQL Agent job that runs in the middle of the night. What is the best practice to force the application to error, because if all the files are not present do not want to process the rest of the jobs steps.

感谢所有帮助.

推荐答案

这取决于您所说的失败".

It depends what you mean by "fail".

有时线条可能会有些模糊,但是例外情况通常只在特殊情况下才会发生.而且一般来说,这意味着您的应用程序崩溃了,应该允许它崩溃.

The lines can be a bit blurry sometimes, but exceptions are something that should generally happen only in, well, exceptional circumstances. And they general mean your application has gone bang and should be allowed to fall over.

如果是这样的情况,例如服务会寻找文件,如果有,文件就会执行某些操作;如果没有,文件则不会执行任何操作(但会继续运行),那么缺少文件也不例外.

If it is a situation where, e.g. a service looks for a file and if it is there it does something and if it isn't then it doesn't do anything (but keeps running), then the lack of a file is not an exception.

您的应用可能应该遵循以下内容:

Rather your app should probably follow the lines of:

if (AreAllFilesPresent()) { ProcessFiles(); Log("All files processed"); } else { Log("Files missing"); }

只有在几种情况下,您才应该捕获并处理异常,如我最喜欢的与异常相关的文章所述 在这里.

There are only a couple of circumstances where you should catch and handle exceptions as explained in my favourite exception-related article here.

更多推荐

强制控制台应用程序失败

本文发布于:2023-11-15 19:24:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!