讲一个经典案例除以0,首先创造一个DivideByZeroEXception类别继承runtime_error。在C++中double除以0并不会报错,所以主动用throw丢出DivideByZeroEXception,并在catch段落呼叫what()方法显示错误原因,最外面用while()产生一个无限回圈
while ( cin >> number1 >> number2 )
{
// try block contains code that might throw exception
// and code that should not execute if an exception occurs
try
{
result = quotient( number1, number2 );
cout << "The quotient is: " << result << endl;
} // end try
// exception handler handles a divide-by-zero exception
catch ( DivideByZeroException ÷ByZeroException )
{
cout << "Exception occurred: "
<< divideByZeroException.what() << endl;
} // end catch
cout << "\nEnter two integers (end-of-file to end): ";
} // end while
详细范例见Fig. 16.1: DivideByZeroException.h与Fig. 16.2: Fig16_02.cpp
<<: [Day - 25] - Spring Reactor Processor 之交易所OrderBook实作与设计
>>: 【Day 25】- 什麽几百张几千张的猫猫图片,戳一戳就结束了(实战 requests 向 API 请求获得回应)
摘要 优化器演算法比较 1.1 浅谈优化器演算法 1.2 设计实验 1.3 函数设定 1.4 纪录学...
实际编程 昨天介绍了PID的理论与原理,最後以下面这个公式收尾 但我们到底要怎麽在程序当中积分、微分...
本文同步更新於blog 前情提要:铁路运输系统,参考范例:运输系统(工厂方法模式) <?p...
这篇文章不再讲下面四件事: 甚麽是 AMP? AMP 对网站流量有帮助吗? 要如何做 AMP 或是 ...
终於到最後一个区域了,我有加一些新的东西在上面这边一起跟大家说 一样先到assets资料夹下的Foo...