如果子方法中发生异常,我正在尝试寻找代码以退出整个方法。我尝试在Subfunction()的catch部分添加return,但是该过程将继续到Thirdfunction()
public static void Mainfunction()
{
try
{
//some code
//some code
Subfunction();
ThirdFunction();
}
catch(Exception ex)
{
//write to log
}
}
public static void Subfunction()
{
try
{
//some code
//some code
}
catch (Exception ex)
{
//write to log
}
}
所以基本上,如果Subfuntion()中发生错误,我想从Mainfunction()中停止该进程,而不继续执行ThirdFunction()。任何帮助将不胜感激。谢谢
答案 0 :(得分:3)
如果Subfuntion()中发生错误,我想停止该过程 来自Mainfunction()
在方法package enums
type SqlQuery string
const (
CreateTable SqlQuery = `CREATE TABELE key.users(id int, email text, title text, content text, magic_number int, PRIMARY KEY(id));`
)
中删除try / catch的最简单方法
如果您想在此方法中保留try / catch(用于日志记录或其他操作),请抛出异常
function adjust_plugin_menu() {
// remove the existing plugin menu items
remove_submenu_page( 'plugins.php', 'plugins.php' ); // Plugins - Installed Plugins
remove_submenu_page( 'plugins.php', 'plugin-install.php' ); // Plugins - Add New Plugins
remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); // Plugins - Editor
// add in your custom menu item
$destination = "path/to/your/plugin/code" ;
add_plugins_page (
'My Plugin Stuff',
'My Plugin Stuff',
'manage_options',
$destination );
// Put the other menu items back in
add_plugins_page('Installed Plugins', 'Installed Plugins', 'manage_options', 'plugins.php');
add_plugins_page('Add New', 'Add New', 'manage_options', 'plugin-install.php');
add_plugins_page('Editor', 'Editor', 'manage_options', 'plugin-editor.php');
}
add_action('admin_menu', 'adjust_plugin_menu');
答案 1 :(得分:2)
基本上有两种可能的解决方案:使用异常和不使用异常。
使用例外,我建议先让它冒起泡沫,就像我在评论中已经说过的那样。
然后您可以重新投掷:
try {
// exception here
}
catch(Exception ex)
{
throw;
}
您可以包装:
try {
// exception here
}
catch(Exception inner)
{
throw new MyCustomException( "Some custom message", inner);
}
顺便说一句:捕获Exception通常不是一个好主意。大多数时候,您想捕获实际上可以处理的特定异常。
另一类解决方案是不冒异常:
返回值:
public static bool Subfunction()
{
bool success = true;
try
{
//some code
//some code
}
catch (Exception ex)
{
success = false;
}
return success;
}
或
private static readonly int DONOTUSEMAGICNUMBERS = 1;
public static int Subfunction()
{
int success = 0;
try
{
//some code
//some code
}
catch (Exception ex)
{
success = DONOTUSEMAGICNUMBERS;
}
return success;
}
尤其是团队中的“ C-Guys”,您可能会偶然发现这一个人。 (没有冒犯-只是我的经验)
或带有状态对象...
public static void Mainfunction()
{
try
{
//some code
//some code
ISuccessIndicator success = new ISIImplementation();
Subfunction( success );
if( !succes.HasException )
{
ThirdFunction();
}
}
catch(Exception ex)
{
//write to log
}
}
public static void Subfunction( ISuccessIndicator result )
{
try
{
//some code
//some code
}
catch (Exception ex)
{
result.HasException=true;
result.Exception = ex;
}
}
public interface ISuccessIndicator
{
Exception Exception {get; set;}
bool HasException {get; set;}
}
如果您真的很疯狂,可以...
public static void Mainfunction()
{
try
{
//some code
//some code
Exception ex = null;
Subfunction( ref ex );
if( ex == null )
{
ThirdFunction();
}
}
catch(Exception ex)
{
//write to log
}
}
public static void Subfunction( ref Exception outEx )
{
try
{
//some code
//some code
}
catch (Exception ex)
{
outEx = ex;
}
}
请注意,我绝不鼓励使用后者。但这是可能 ...,OP要求可能性。
免责声明:所有摘要未经测试。谁发现错误可以保留它们(但是请写一条评论,以便我纠正它们)。