1002. Multiple exceptions

本文通过一个具体的异常处理案例,演示了如何使用C++捕获不同类型的异常。案例中定义了一个名为multi_throw的函数,该函数根据输入整数的不同范围抛出各种异常,包括logic_error、runtime_error和out_of_range等。主程序部分不断读取输入并调用此函数,通过多个catch块来捕获这些异常,并输出异常信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Time Limit: 1sec    Memory Limit:256MB
Description
 
Given the following multi_throw function, you are asked to write a program to catch all the exceptions. The program first reads an integer, passes it to the multi_throw function, and catches the exception that it may throw.
 
void multi_throw(int i)
{
     cout << "Input: " << i << endl;
     if (i <= 0) throw logic_error("non-positive");
     if (i <= 1) throw i;
     if (i <= 5) throw runtime_error("run error");
     if (i >= 9)  throw out_of_range("too big");
}
 
Your submitted source code should not include the multi_throw function.
Input
Input contains multiple test cases. Each case consists of an integer on a single line. Process to the end of file.
Output
For each test case, ouput the exception message and the type of exception if an exception is catched. Please follow the formats of the sample output.
Sample Input
 Copy sample input to clipboard
-1
1
9
Sample Output
Input: -1
Exception: non-positive
Exception type: class logic_error
Input: 1
Exception: 1
Exception type: int
Input: 9
Exception: too big
Exception type: class out_of_range

一发WA:

#include<iostream>
#include<cmath>
#include<stdexcept>
#include<cstring>
#include "string" 
#include <stdlib.h>
#include <typeinfo>
using namespace std;
void multi_throw(int i)
{
     cout << "Input: " << i << endl;
     if (i <= 0) throw logic_error("non-positive");
     if (i <= 1) throw i;
     if (i <= 5) throw runtime_error("run error");
     if (i >= 9)  throw out_of_range("too big");
}
int main(){
    
    int s;
    cin>>s;
     try{
     
         multi_throw(s);
     }
      catch(logic_error &lx){
     cout<<"Exception: "<<lx.what()<<endl;
     cout<<"Exception type: class logic_error"<<endl;
     }
     catch(int ix){
     cout<<"Exception: "<<ix<<endl;
     cout<<"Exception type: int"<<endl;
     }
     
    
    
      catch(runtime_error &rx){
     cout<<"Exception: "<<rx.what()<<endl;
     cout<<"Exception type: class runtime_error"<<endl;
     }
     catch(out_of_range &ox){
     cout<<"Exception: "<<ox.what()<<endl;
     cout<<"Exception type: class out_of_range"<<endl;
     }
     }
     
    

AC:文件结束While(cin>>x)以CTRL+Z结束       catch的顺序?

#include<iostream>
#include<cmath>
#include<stdexcept>
#include<cstring>
#include "string" 
#include <stdlib.h>
#include <typeinfo>
using namespace std;
/*void multi_throw(int i)
{
     cout << "Input: " << i << endl;
     if (i <= 0) throw logic_error("non-positive");
     if (i <= 1) throw i;
     if (i <= 5) throw runtime_error("run error");
     if (i >= 9)  throw out_of_range("too big");
}*/
int main(){
    
    int s;
    while(cin>>s){
    
    
     try{
     
         multi_throw(s);
     }
      
     catch(int ix){
     cout<<"Exception: "<<ix<<endl;
     cout<<"Exception type: int"<<endl;
     }
     
    
    
      catch(runtime_error &rx){
     cout<<"Exception: "<<rx.what()<<endl;
     cout<<"Exception type: class runtime_error"<<endl;
     }
     catch(out_of_range &ox){
     cout<<"Exception: "<<ox.what()<<endl;
     cout<<"Exception type: class out_of_range"<<endl;
     }
     catch(logic_error &lx){
     cout<<"Exception: "<<lx.what()<<endl;
     cout<<"Exception type: class logic_error"<<endl;
     }
     }
     return 0;
 }
    


conclusion:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值