473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Exception handling

62
Hi,

I tried the next simple code for catch exception:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void fn(int a) throw(double){
  5.     cout << "function call" << endl;
  6. }
  7.  
  8. void main(void){
  9.         double d=3.97
  10.     try{
  11.         fn(d);
  12.     }
  13.     catch(double){
  14.         cout << "wrong argument" << endl;
  15.     }
  16.  
  17.     cin.get();
  18. }
But I have not got the message "wrong argument" on output.I am using VS 2008 Express Edition. Could someone explain what would be wrong?

Thanks

Arepi
Sep 24 '10 #1

✓ answered by hype261

Your problem lies here...

Expand|Select|Wrap|Line Numbers
  1. void fn(int a) throw(double) 
Basically what this is telling the compiler that this function can only throw exceptions with type double. If an exception is generated that doesn't match this type then it is converted to unexpected I believe.

What you are looking for is something like this

Expand|Select|Wrap|Line Numbers
  1. void fn(double a) throw(double) <--- the parameter needs to be double not int
  2. {
  3.  throw a;
  4. }

1 973
hype261
207 100+
Your problem lies here...

Expand|Select|Wrap|Line Numbers
  1. void fn(int a) throw(double) 
Basically what this is telling the compiler that this function can only throw exceptions with type double. If an exception is generated that doesn't match this type then it is converted to unexpected I believe.

What you are looking for is something like this

Expand|Select|Wrap|Line Numbers
  1. void fn(double a) throw(double) <--- the parameter needs to be double not int
  2. {
  3.  throw a;
  4. }
Sep 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
13
by: junw2000 | last post by:
Is C++ Exception handling useful? think it is too complicated. What kinds of project need to use it? Thanks.
3
by: Larry Herbinaux | last post by:
I have built an asychronous TCP Server that uses the thread pool. I have two levels of exception handling in case the handling of the inner catch block throws an exception. The outer catch block...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.