Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中放置自定义错误消息_Python_Validation_Python 2.7 - Fatal编程技术网

在Python中放置自定义错误消息

在Python中放置自定义错误消息,python,validation,python-2.7,Python,Validation,Python 2.7,我想为函数创建自定义错误消息 def tr( launch_speed , launch_angle_deg , num_samples ): #Error displays try: launch_speed>0 except: raise Exception("Launch speed has to be positive!") try: 0<launch_angle_deg<90 except:

我想为函数创建自定义错误消息

def tr( launch_speed , launch_angle_deg , num_samples ):

#Error displays
   try:
      launch_speed>0
   except:
      raise Exception("Launch speed has to be positive!")   
   try:
      0<launch_angle_deg<90
   except:
      raise Exception("Launch angle has to be 0 to 90 degrees!")    
   try:
      um_samples = int(input())
   except:
      raise Exception("Integer amount of samples!")  
   try:
      num_samples >=2
   except:
      raise Exception("At least 2 samples!")    
def tr(启动速度、启动角度、样本数):
#错误显示
尝试:
启动速度>0
除:
raise异常(“启动速度必须为正!”)
尝试:

0您不能使用
try:除了:
用于所有内容;例如,
launch\u speed>0
不会产生负值错误。相反,我认为你想要

if launch_speed < 0:  # note spacing, and if not try
    raise ValueError("Launch speed must be positive.")  # note specific error

您可以看到内置错误的列表。

您不能使用
try:except:
进行所有操作;例如,
launch\u speed>0
不会产生负值错误。相反,我认为你想要

if launch_speed < 0:  # note spacing, and if not try
    raise ValueError("Launch speed must be positive.")  # note specific error

您可以看到内置错误的列表。

您不能使用
try:except:
进行所有操作;例如,
launch\u speed>0
不会产生负值错误。相反,我认为你想要

if launch_speed < 0:  # note spacing, and if not try
    raise ValueError("Launch speed must be positive.")  # note specific error

您可以看到内置错误的列表。

您不能使用
try:except:
进行所有操作;例如,
launch\u speed>0
不会产生负值错误。相反,我认为你想要

if launch_speed < 0:  # note spacing, and if not try
    raise ValueError("Launch speed must be positive.")  # note specific error

您可以看到内置错误列表。

为什么不进一步构建您自己的异常类型?有一个快速教程,可以使用如下内容:

class Error(Exception):
    """Base class for exceptions defined in this module"""
    pass

class LaunchError(Error):
    """Errors related to the launch"""
    pass

class LaunchSpeedError(LaunchError):
    """Launch speed is wrong"""
    pass

class LaunchAngleError(LaunchError):
    """Launch angle is wrong"""
    pass

class SamplesError(Error):
    """Error relating to samples"""
    pass
在这种情况下,
Exception
的默认功能很好,但是您可以通过定义额外的异常来获得更精细的捕获粒度

if launch_speed < 0:
    raise LaunchSpeedError("Launch speed must be positive")
if 0 <= launch_angle < 90:
    raise LaunchAngleError("Launch angle must be between 0 and 90")
um_samples = input()
try:
    um_samples = int(um_samples)
except ValueError:
    raise SampleError("Samples must be an integer, not {}".format(um_samples))
if um_samples < 2:
    raise SampleError("Must include more than one sample, not {}".format(str(um_samples)))
如果启动速度<0:
提升启动速度错误(“启动速度必须为正”)

如果0为什么不进一步构建自己的异常类型?有一个快速教程,可以使用如下内容:

class Error(Exception):
    """Base class for exceptions defined in this module"""
    pass

class LaunchError(Error):
    """Errors related to the launch"""
    pass

class LaunchSpeedError(LaunchError):
    """Launch speed is wrong"""
    pass

class LaunchAngleError(LaunchError):
    """Launch angle is wrong"""
    pass

class SamplesError(Error):
    """Error relating to samples"""
    pass
在这种情况下,
Exception
的默认功能很好,但是您可以通过定义额外的异常来获得更精细的捕获粒度

if launch_speed < 0:
    raise LaunchSpeedError("Launch speed must be positive")
if 0 <= launch_angle < 90:
    raise LaunchAngleError("Launch angle must be between 0 and 90")
um_samples = input()
try:
    um_samples = int(um_samples)
except ValueError:
    raise SampleError("Samples must be an integer, not {}".format(um_samples))
if um_samples < 2:
    raise SampleError("Must include more than one sample, not {}".format(str(um_samples)))
如果启动速度<0:
提升启动速度错误(“启动速度必须为正”)

如果0为什么不进一步构建自己的异常类型?有一个快速教程,可以使用如下内容:

class Error(Exception):
    """Base class for exceptions defined in this module"""
    pass

class LaunchError(Error):
    """Errors related to the launch"""
    pass

class LaunchSpeedError(LaunchError):
    """Launch speed is wrong"""
    pass

class LaunchAngleError(LaunchError):
    """Launch angle is wrong"""
    pass

class SamplesError(Error):
    """Error relating to samples"""
    pass
在这种情况下,
Exception
的默认功能很好,但是您可以通过定义额外的异常来获得更精细的捕获粒度

if launch_speed < 0:
    raise LaunchSpeedError("Launch speed must be positive")
if 0 <= launch_angle < 90:
    raise LaunchAngleError("Launch angle must be between 0 and 90")
um_samples = input()
try:
    um_samples = int(um_samples)
except ValueError:
    raise SampleError("Samples must be an integer, not {}".format(um_samples))
if um_samples < 2:
    raise SampleError("Must include more than one sample, not {}".format(str(um_samples)))
如果启动速度<0:
提升启动速度错误(“启动速度必须为正”)

如果0为什么不进一步构建自己的异常类型?有一个快速教程,可以使用如下内容:

class Error(Exception):
    """Base class for exceptions defined in this module"""
    pass

class LaunchError(Error):
    """Errors related to the launch"""
    pass

class LaunchSpeedError(LaunchError):
    """Launch speed is wrong"""
    pass

class LaunchAngleError(LaunchError):
    """Launch angle is wrong"""
    pass

class SamplesError(Error):
    """Error relating to samples"""
    pass
在这种情况下,
Exception
的默认功能很好,但是您可以通过定义额外的异常来获得更精细的捕获粒度

if launch_speed < 0:
    raise LaunchSpeedError("Launch speed must be positive")
if 0 <= launch_angle < 90:
    raise LaunchAngleError("Launch angle must be between 0 and 90")
um_samples = input()
try:
    um_samples = int(um_samples)
except ValueError:
    raise SampleError("Samples must be an integer, not {}".format(um_samples))
if um_samples < 2:
    raise SampleError("Must include more than one sample, not {}".format(str(um_samples)))
如果启动速度<0:
提升启动速度错误(“启动速度必须为正”)


如果0,您能比“它似乎不起作用”更具体一些吗?为什么你期望例如
发射速度>0
会产生错误?我正在构建一个计算投射物轨迹的函数,所以我不希望发射速度为负值。你能比“它似乎不起作用”更具体一些吗?为什么你期望例如
发射速度>0
会产生错误?我正在构建一个计算投射物轨迹的函数,所以我不希望发射速度为负值。你能比“它似乎不起作用”更具体一些吗?为什么你期望例如
发射速度>0
会产生错误?我正在构建一个计算投射物轨迹的函数,所以我不希望发射速度为负值。你能比“它似乎不起作用”更具体一些吗?为什么你希望例如
launch_speed>0
会出现错误?我正在构建一个函数来计算投射物的轨迹,所以我不希望发射速度为负值。我尝试使用第二个函数来计算num_样本,但即使我输入例如5.5,我将得到函数的结果,而不是错误。@George您应该使用
原始输入
,而不是
输入
;我已经相应地更新了答案。@George如果你特别想在用户给你一个非整数时抛出一个错误,你应该执行类似于
user\u in=raw\u input()的操作;if int(用户输入)!=浮动(用户输入):引发一些错误(“带有自定义消息”)
。然而,对我来说,把它转换成可用的东西并使用它似乎更有用,只是抱怨它不能转换成
int
,所以我试着用第二个来处理num_示例,但即使我输入了示例5.5,我也会得到函数的结果,而不是错误。@George你应该使用
raw_input
,不
输入
;我已经相应地更新了答案。@George如果你特别想在用户给你一个非整数时抛出一个错误,你应该执行类似于
user\u in=raw\u input()的操作;if int(用户输入)!=浮动(用户输入):引发一些错误(“带有自定义消息”)
。然而,对我来说,把它转换成可用的东西并使用它似乎更有用,只是抱怨它不能转换成
int
,所以我试着用第二个来处理num_示例,但即使我输入了示例5.5,我也会得到函数的结果,而不是错误。@George你应该使用
raw_input
,不
输入
;我已经相应地更新了答案。@George如果你特别想在用户给你一个非整数时抛出一个错误,你应该执行类似于
user\u in=raw\u input()的操作;if int(用户输入)!=浮动(用户输入):引发一些错误(“带有自定义消息”)
。然而,对我来说,把它转换成可用的东西并使用它似乎更有用,只是抱怨它不能转换成
int
,所以我试着用第二个来处理num_示例,但即使我输入了示例5.5,我也会得到函数的结果,而不是错误。@George你应该使用
raw_input
,不
输入
;我已经相应地更新了答案。@George If