无法弄清楚我自己的自定义异常
Cant figure out my own custom exception
我有这个 try catch 块。我对编写自己的异常比较陌生,因此请纠正我做错的任何事情。现在我的问题是我想弄清楚是否缺少两件事之一并将该名称传递给我的异常。但似乎我在进行任何检查之前就退出了尝试。这是代码:
string componentName = "";
try
{
if (Constants.AllowedTyleTypes.Contains(currentTile.tyleType))
{
GameObject childSprite = currentTile.transform.FindChild("Sprite").gameObject;
Animator spriteAnimator = childSprite.GetComponent<Animator>();
spriteAnimator.SetBool("PassedBy", true);
}
}
catch(System.Exception)
{
throw new MissingComponentsForAnimationException(componentName, currentTile.name);
}
我要检查的两件事是 childSprite 和 spriteAnimator。
您应该将编码更改为以下内容,并对正确的异常/组件名称进行空检查:
string componentName = "";
if (Constants.AllowedTyleTypes.Contains(currentTile.tyleType))
{
if (currentTile.transform.FindChild("Sprite") == null || currentTile.transform.FindChild("Sprite").gameObject == null)
{
throw new MissingComponentsForAnimationException("childSprite", currentTile.name);
}
GameObject childSprite = currentTile.transform.FindChild("Sprite").gameObject;
Animator spriteAnimator = childSprite.GetComponent<Animator>();
if (spriteAnimator == null)
{
throw new MissingComponentsForAnimationException("spriteAnimator", currentTile.name);
}
spriteAnimator.SetBool("PassedBy", true);
}
在您的代码中,您试图捕获 System.Exception
,而根据您所说的,您实际上想要抛出 MissingComponentsForAnimationException
。
如果 childSprite 或 spriteAnimator 为 null,下面的代码将抛出您想要的异常。然后由您决定如何处理它们。
if (Constants.AllowedTyleTypes.Contains(currentTile.tyleType))
{
GameObject childSprite = currentTile.transform.FindChild("Sprite").gameObject;
if (childSprite == null)
throw new MissingComponentsForAnimationException("childSprite", currentTile.Name);
Animator spriteAnimator = childSprite.GetComponent<Animator>();
if (spriteAnimator == null)
throw new MissingComponentsForAnimationException("spriteAnimator", currentTile.Name);
spriteAnimator.SetBool("PassedBy", true);
}
我有这个 try catch 块。我对编写自己的异常比较陌生,因此请纠正我做错的任何事情。现在我的问题是我想弄清楚是否缺少两件事之一并将该名称传递给我的异常。但似乎我在进行任何检查之前就退出了尝试。这是代码:
string componentName = "";
try
{
if (Constants.AllowedTyleTypes.Contains(currentTile.tyleType))
{
GameObject childSprite = currentTile.transform.FindChild("Sprite").gameObject;
Animator spriteAnimator = childSprite.GetComponent<Animator>();
spriteAnimator.SetBool("PassedBy", true);
}
}
catch(System.Exception)
{
throw new MissingComponentsForAnimationException(componentName, currentTile.name);
}
我要检查的两件事是 childSprite 和 spriteAnimator。
您应该将编码更改为以下内容,并对正确的异常/组件名称进行空检查:
string componentName = "";
if (Constants.AllowedTyleTypes.Contains(currentTile.tyleType))
{
if (currentTile.transform.FindChild("Sprite") == null || currentTile.transform.FindChild("Sprite").gameObject == null)
{
throw new MissingComponentsForAnimationException("childSprite", currentTile.name);
}
GameObject childSprite = currentTile.transform.FindChild("Sprite").gameObject;
Animator spriteAnimator = childSprite.GetComponent<Animator>();
if (spriteAnimator == null)
{
throw new MissingComponentsForAnimationException("spriteAnimator", currentTile.name);
}
spriteAnimator.SetBool("PassedBy", true);
}
在您的代码中,您试图捕获 System.Exception
,而根据您所说的,您实际上想要抛出 MissingComponentsForAnimationException
。
如果 childSprite 或 spriteAnimator 为 null,下面的代码将抛出您想要的异常。然后由您决定如何处理它们。
if (Constants.AllowedTyleTypes.Contains(currentTile.tyleType))
{
GameObject childSprite = currentTile.transform.FindChild("Sprite").gameObject;
if (childSprite == null)
throw new MissingComponentsForAnimationException("childSprite", currentTile.Name);
Animator spriteAnimator = childSprite.GetComponent<Animator>();
if (spriteAnimator == null)
throw new MissingComponentsForAnimationException("spriteAnimator", currentTile.Name);
spriteAnimator.SetBool("PassedBy", true);
}