🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: be.comp.programming
2 messages
2 total messages Started by Manchiu Fri, 25 May 2007 12:53
excepties C++
#415
Author: Manchiu
Date: Fri, 25 May 2007 12:53
29 lines
1411 bytes
Hierbij bij ingave negatief getal wordt ik eruit gegooid, hoe komt dit ??

#include <iostream>
using namespace std;

int main()

{
 int li_getal;

 cout << "Geef een getal in > 0: ";
 try
 {
  cin >> li_getal;
 }

 catch(int)
 {
  cerr << "Is kleiner dan 0" << endl;
 }

 if(li_getal<0)
 {
   throw 1;
 }

 return 0;

} 
Re: excepties C++
#416
Author: Raf
Date: Fri, 25 May 2007 21:16
46 lines
1805 bytes
Manchiu <manchiu@nomail.com> wrote:

> Hierbij bij ingave negatief getal wordt ik eruit gegooid, hoe komt dit ??

Wat je in je functie (main() in dit geval) plaatst, wordt gewoon na
elkaar uitgevoerd. Dit:

>  if(li_getal<0)
>  {
>    throw 1;
>  }

wordt dus nergens gecatcht, want die try...catch is al voorbij. Ik denk
dat je +- zoiets wil:

#include <iostream>
using namespace std;

void get_number()
{
  int li_getal;

  cout << "Geef een getal in > 0: ";
  cin >> li_getal;
  if (li_getal < 0) {
    throw li_getal;
  }
}

int main()
{
  try {
    get_number();
  }
  catch(int) {
    cerr << "Is kleiner dan 0" << endl;
  }

  return 0;
}

Overigens wordt 0 nu ook goedgekeurd, dus je zou beter vragen naar
een getal >= 0 :-)

-- 
Raf
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads