尝试,投掷,捕捉功能

时间:2017-05-17 16:46:07

标签: c++

我有几个问题。首先,我会给出一些伪代码,我想用这些东西。

我正在做一些功课(银行的实现),我有下一个功能

void MoneyMarketing_Account::Transaction_menu()
{
  int choice, value;
  string login, password;
  cout << "Enter name and pas for account ";
  cin >> login, password;
  cout << "Choose valute and amount" << endl
     << "1 Grn     [amount]" << endl
     << "2 Dollars [amount]" << endl
     << "3 Euro    [amount]" << endl
     << "4 Forint  [amount]" << endl
     << "5 Rub     [amount]" << endl;
  cin >> choice;
  cin >> value;
 if(choice == 1)
     throw(Request_t(login, password, "Grn", value));
 if(choice == 2)
     throw(Request_t(login, password, "Dollars", value));
 if (choice == 3)
     throw(Request_t(login, password, "Euro", value));
 if (choice == 4)
     throw(Request_t(login, password, "Forint", value));
 if (choice == 5)
     throw(Request_t(login, password, "Rub", value));
 struct Request_t {
  string login;
  string password;
  string type;
  int amount;
  Request_t(string login_, string password_, string type_, int amount_) {
    login = login_;
    password = password_;
    type = type_;
    amount = amount_;
}};

第一个问题是 - 我可以使用throw语句而不使用try块。  第二个是通过的地方。例如

void foo()
{
  Transaction_menu(); //function that i have shown before 
  Transaction_catch();//function that is supposed to catch request
}

因此在第一个函数中调用的这个throw会进入第二个函数吗?

2 个答案:

答案 0 :(得分:3)

  

我可以使用throw语句而不使用try block

是。您的namespace Services { class LogService { info(message: string, ...optionalParams: any[]) { if (optionalParams && optionalParams.length > 0) { console.log(message, optionalParams); return; } console.log(message); } } } class ExampleSystemUnderTest { constructor(private log: Services.LogService) { } doIt() { this.log.info('done'); } } // I export this in a common test file // with other utils that all tests import const asSpy = f => <jasmine.Spy>f; describe('SomeTest', () => { let log: Services.LogService; let sut: ExampleSystemUnderTest; // ARRANGE beforeEach(() => { log = jasmine.createSpyObj('log', ['info', 'error']); sut = new ExampleSystemUnderTest(log); }); it('should do', () => { // ACT sut.doIt(); // ASSERT expect(asSpy(log.error)).not.toHaveBeenCalled(); expect(asSpy(log.info)).toHaveBeenCalledTimes(1); expect(asSpy(log.info).calls.allArgs()).toEqual([ ['done'] ]); }); }); 功能完全有效,但是......

  

...在第一个函数中调用此抛出是否会进入第二个函数?

没有

如果foo()以您展示的方式调用foo(),那么当Transaction_menu()引发异常时,它也会立即导致Transaction_menu() 扔掉它。

如果你想捕获异常,你必须这样做:

foo()

P.S。; &#34; Request_t&#34;是您打算void foo() { try { Transaction_menu(); } catch (Request_t &request) { ...code that goes here will run ONLY if a Request_t is thrown,... ...and request will be a reference to the thrown object... } } 的数据类型的有趣名称。通常抛出的类型的名称类似于throw

P.P.S .;因为某些错误而抛出异常是非常不寻常的。这将使其他程序员更难以阅读和理解您的代码,因为它完全违背了他们的期望。

此外,TransactionExceptionthrow更昂贵,因此在性能至关重要的应用程序中,这是除了错误之外不会因任何其他原因而抛出的另一个原因。

答案 1 :(得分:2)

添加@james answer(技术上正确):

永远不会使用异常来实现代码中的控制流程 它们用于特殊情况和错误情况,这些情况在抛出它们的代码段中无法涵盖。

您最想要的是一个简单的返回值:

Request_t MoneyMarketing_Account::Transaction_menu()
{
 // ...
 if(choice == 1)
     return Request_t(login, password, "Grn", value);
 if(choice == 2)
     return Request_t(login, password, "Dollars", value);
 if (choice == 3)
     return Request_t(login, password, "Euro", value);
 if (choice == 4)
     return Request_t(login, password, "Forint", value);
 if (choice == 5)
     return Request_t(login, password, "Rub", value);

 throw std::runtime_error("Invalid choice");
}