SVector.H:
void pop_back() throw (underflow_error);
Run Code Online (Sandbox Code Playgroud)
在我的SVector.cpp文件中,我是否还应该包含throw(underflow_error)部分?
void pop_back() throw (underflow_error)
{
// implementation
}
Run Code Online (Sandbox Code Playgroud)
要么
void pop_back()
{
// implementation
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
可能重复:
为什么在C#中捕获并重新抛出异常?
我一直在寻找网络,试图找到这个问题的答案 - 两者之间有什么关系?
try
{
//Do Something
}
catch
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
VS
try
{
//Do Something
}
catch
{
}
Run Code Online (Sandbox Code Playgroud)
要么
try
{
//Do Something
}
catch (Exception Ex)
{
//Do something with Exception Ex
}
Run Code Online (Sandbox Code Playgroud) 我试图使用一个类来抛出异常,但异常虽然必须抛出一个值.我如何创建类,以便在抛出异常时它携带一个值到处理程序?
码:
catch (IOException e) {
LOGGER.error("IOException exception happened");
//now need throw again the same exception to be
//catched in the upper method
}
Run Code Online (Sandbox Code Playgroud)
但当我尝试简单:
catch (IOException e) {
LOGGER.error("IOException exception happened");
//now need throw again the same exception to be
//catched in the upper method
throw e;
}
Run Code Online (Sandbox Code Playgroud)
Eclipse假设我把"throw e"放在try catch块中.但这是无稽之谈.怎么解决这个问题?谢谢.
我有一段代码让我发疯。一般流程是,当 TRY 中的某个事件发生时,我抛出异常...根据我的理解,每当调用 时throw,它只是停止在同一个类中进一步执行,并将控制权从该类的函数所在的位置返回叫...
这是代码...
try{
session = getHibernateSession();
companyAccountLinkingWSBean = (CompanyAccountLinkingWSBean) wsAttribute
.getBeanObject();
companyToMatch = companyAccountLinkingWSBean.getCompanyCode();
cnicToMatch = companyAccountLinkingWSBean.getCnic();
LOG.debug("We have found the Mobile number from the WS Bean as input");
mobile = companyAccountLinkingWSBean.getMobileNumber();
LOG.info("Mobile is : " + mobile);
if(mobile.isEmpty()){
LOG.info("Coming in mobile.isEmpty()");
companyResponceWSBean = new CompanyResponceWSBean();
companyResponceWSBean.setpID(Constants.INPUT_MOBILE_ERROR);
companyResponceWSBean.setMessage(Constants.INPUT_MOBILE_ERROR_MSG);
companyResponceWSBean.setSuccess(false);
response = new WSAttribute();
response.setBeanObject(companyResponceWSBean);
LOG.info("BEFORE THROWING");
throw new PayboxFault(Constants.INPUT_MOBILE_ERROR,
Constants.INPUT_MOBILE_ERROR_MSG);
}
LOG.info("Out side IF statement!!");
} catch (Exception e) {
LOG.info("IN Exception!!");
}
LOG.info("Out Side …Run Code Online (Sandbox Code Playgroud) 我试图理解throw和throw ex之间的区别。为了帮助我的学习,我对理论进行了一些研究,将我带到以下链接:
总结以上几点,区别是:
throw重新引发捕获的异常,并保留堆栈跟踪。throw ex引发相同的异常,但是将堆栈跟踪重置为该方法。
因此,我继续为同一个应用程序创建了一个演示应用程序,以了解操作上的差异。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Calc c = new Calc();
c.Test();
}
}
class Calc
{
public void Test()
{
try
{
int a = 10;
int b = 10;
int c = 10 / (a - b);
}
catch (Exception ex)
{
throw; //throw statement
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给我的输出为:-
未处理的异常:System.DivideByZeroException:试图除以零。在C:\ kgn \ personal \ workspace \ ConsoleApplication1 \ ConsoleApplication1 …
我是 C# 新手,正在练习抛出异常。从辅助方法抛出异常以缩短所需的代码量是一种好习惯吗?像这样:
public static void ThrowExcIfNull<T>(T[] array)
{
if (array == null) throw new ArgumentNullException("Array is null");
}
/// <summary>
/// Does Something
/// </summary>
/// <param name="x">The int array to be used</param>
/// <exception cref="ArgumentNullException">Thrown when the string is
/// null</exception> //Is this correct?
/// <returns>Some integer</returns>
public static int SomeMethod(this int[] x)
{
ThrowExcIfNull(x);
//Some code here
}
Run Code Online (Sandbox Code Playgroud)
另外,是否可以编写文档说“从 someMethod 抛出异常”?任何信息都会有所帮助!谢谢
我有这个代码片段:
class Program
{
public static async Task ProcessAsync(string s)
{
Console.WriteLine("call function");
if (s == null)
{
Console.WriteLine("throw");
throw new ArgumentNullException("s");
}
Console.WriteLine("print");
await Task.Run(() => Console.WriteLine(s));
Console.WriteLine("end");
}
public static void Main(string[] args)
{
try
{
ProcessAsync(null);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它运行并打印:
call function
throw
Run Code Online (Sandbox Code Playgroud)
好的,抛出异常,但是主函数的 try/catch 无法捕获异常,如果我删除 try/catch,main 也不会报告未处理的异常。这很奇怪,我用谷歌搜索,它说 [await] 中有陷阱,但没有解释如何以及为什么。
所以我的问题是,为什么这里没有捕获异常,使用 await 的陷阱是什么?
非常感谢。
这可能是显而易见的,但我还是不明白之间的差别throw,并rethrow和那些既做时,应使用?
...需要一致的实现来模拟(仅)抽象机的可观察行为,如下所述。
这个解释就是as-if 规则,其中包含以下示例:
...如果实际实现可以推断出其值未被使用并且不会产生影响程序可观察行为的副作用,则实际实现不需要评估表达式的一部分。
副作用的定义在intro.execution#7中:
读取由易失性泛左值 ([basic.lval]) 指定的对象、修改对象、调用库 I/O 函数或调用执行任何这些操作的函数都是副作用,即状态的更改执行环境。...
在我看来,在以下程序中:
int main()
{
throw 42;
}
Run Code Online (Sandbox Code Playgroud)
表达式的值throw 42;未被使用,并且它不满足任何副作用的标准。
这是否意味着允许实现不评估该表达式?上述程序是否等价于:
int main() {}
Run Code Online (Sandbox Code Playgroud)
就抽象机而言?我找不到任何说明抽象机器知道或关心异常的文本。
throw ×10
exception ×6
c# ×4
c++ ×3
try-catch ×3
java ×2
arguments ×1
asp.net ×1
async-await ×1
asynchronous ×1
dart ×1
flutter ×1
ioexception ×1
methods ×1
propagation ×1
semantics ×1
syntax ×1