473,789 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing Exception Catching At Compile Time

hi,

is there a way to force the user of an object to catch exceptions of
specific type?

for instance a compile error is issued if exception 'ExeceptionXXX' thrown
by method 'ThrowsExceptio nXXX' is not caught.
thanks,
bryan
Jul 22 '05 #1
17 3147

"Bryan Bullard" <th************ ********@sbcglo bal.net> wrote in message news:aj******** *********@newss vr23.news.prodi gy.com...
hi,

is there a way to force the user of an object to catch exceptions of
specific type?

for instance a compile error is issued if exception 'ExeceptionXXX' thrown
by method 'ThrowsExceptio nXXX' is not caught.

No way to do that. Exceptions need not be caught in the function
that throws them, or even one level up (if that were the case, they'd
have little purpose, you'd just return instead).
Jul 22 '05 #2

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

ron-

no, not in the function that throws them. i'm not sure if my question was
clear.

i want to know if there is a way to force the compiler to check to see if an
exception is throwable by the method in question. if a call to that method
is not in a try/catch block a compile time error is produced.

similar to java.

thanks,
bryan


"Bryan Bullard" <th************ ********@sbcglo bal.net> wrote in message

news:aj******** *********@newss vr23.news.prodi gy.com...
hi,

is there a way to force the user of an object to catch exceptions of
specific type?

for instance a compile error is issued if exception 'ExeceptionXXX' thrown by method 'ThrowsExceptio nXXX' is not caught.

No way to do that. Exceptions need not be caught in the function
that throws them, or even one level up (if that were the case, they'd
have little purpose, you'd just return instead).

Jul 22 '05 #3
In article <Zz************ *****@newssvr23 .news.prodigy.c om>, Bryan Bullard wrote:

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

ron-

no, not in the function that throws them. i'm not sure if my question was
clear.

i want to know if there is a way to force the compiler to check to see if an
exception is throwable by the method in question. if a call to that method
is not in a try/catch block a compile time error is produced.

similar to java.


C++ does not have checked exceptions. It has "exception specifications" , which
are not the same thing. You can google on this topic if you like. There's a
widespread feeling in the C++ community that exception specifications, as implemented
in C++, are not very helpful.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #4

"Donovan Rebbechi" <ab***@aol.co m> wrote in message
news:sl******** **********@pani x2.panix.com...

thanks for your post.

-bryan
In article <Zz************ *****@newssvr23 .news.prodigy.c om>, Bryan Bullard wrote:

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

ron-

no, not in the function that throws them. i'm not sure if my question was clear.

i want to know if there is a way to force the compiler to check to see if an exception is throwable by the method in question. if a call to that method is not in a try/catch block a compile time error is produced.

similar to java.


C++ does not have checked exceptions. It has "exception specifications" ,

which are not the same thing. You can google on this topic if you like. There's a widespread feeling in the C++ community that exception specifications, as implemented in C++, are not very helpful.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/

Jul 22 '05 #5

"Bryan Bullard" <th************ ********@sbcglo bal.net> wrote in message news:Zz******** *********@newss vr23.news.prodi gy.com...

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

ron-

no, not in the function that throws them. i'm not sure if my question was
clear.

i want to know if there is a way to force the compiler to check to see if an
exception is throwable by the method in question. if a call to that method
is not in a try/catch block a compile time error is produced.

But how is it supposed to do that? The try/catch block may be some
distance up the call stack and perhaps in a completely different translation
unit.

a.cc
int main() {
void foo();
try { foo(); } catch { exit(1); }
}

b.cc
void goo() {
throw "BLETCH";
};

void foo() {
goo();
}

So when the compiler calls b.c it's supposed to complain that foo() doesn't
catch the exepction? It is caught, but in another tu.

Jul 22 '05 #6
In article <3f************ ***********@new s.newshosting.c om>, Ron Natalie wrote:

"Bryan Bullard" <th************ ********@sbcglo bal.net> wrote in message news:Zz******** *********@newss vr23.news.prodi gy.com...

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

ron-

no, not in the function that throws them. i'm not sure if my question was
clear.

i want to know if there is a way to force the compiler to check to see if an
exception is throwable by the method in question. if a call to that method
is not in a try/catch block a compile time error is produced.
But how is it supposed to do that?


The point is that you combine it with something similar to exception
specifications: you either need to specificy that the function throws
the given exception, or you need to catch it.
The try/catch block may be some
distance up the call stack and perhaps in a completely different translation
unit.
the caller of the function that issues the throw statement needs to either
catch the exception or include a specification in its declaration.

the caller of a function that includes such a specification needs to do
the same (use a try/catch or an exception specification).

The up-the-stack thing follows by induction.
a.cc
int main() {
void foo();

should be (in this way of looking at things, which doesn't work in C++):
void foo () throw (const char*);

I'm not sure why C++ doesn't have specifications implemented in this
manner.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #7

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Bryan Bullard" <th************ ********@sbcglo bal.net> wrote in message news:Zz******** *********@newss vr23.news.prodi gy.com...

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

ron-

no, not in the function that throws them. i'm not sure if my question was clear.

i want to know if there is a way to force the compiler to check to see if an exception is throwable by the method in question. if a call to that method is not in a try/catch block a compile time error is produced.

But how is it supposed to do that? The try/catch block may be some
distance up the call stack and perhaps in a completely different

translation unit.
which is fine if all the headers are there and there was support for it in
the language. a method would have to be declared to thrown an exception of
a certain type.

a.cc
int main() {
void foo();
try { foo(); } catch { exit(1); }
}

just as mr. rebbechi mentioned, c++ don't support exception checking.
in an exception checking scenario, the compiler would give an error if foo
is not called in a try block that catches char* or something more generic
(...) if goo or foo is declared to throw char* (or "BLETCH" as you say)

-bryan

b.cc
void goo() {
throw "BLETCH";
};

void foo() {
goo();
}

So when the compiler calls b.c it's supposed to complain that foo() doesn't catch the exepction? It is caught, but in another tu.

Jul 22 '05 #8

"Donovan Rebbechi" <ab***@aol.co m> wrote in message news:sl******** **********@pani x2.panix.com...
the caller of the function that issues the throw statement needs to either
catch the exception or include a specification in its declaration.
Why. By not having an exception specification, the function is free to throw
any exception.

I'm not sure why C++ doesn't have specifications implemented in this
manner.

Because the compiler is a little prejudiced against enforcing things across
translation units.

Jul 22 '05 #9

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Donovan Rebbechi" <ab***@aol.co m> wrote in message news:sl******** **********@pani x2.panix.com...
the caller of the function that issues the throw statement needs to either catch the exception or include a specification in its declaration.
Why. By not having an exception specification, the function is free to

throw any exception.
can you not see the inherent problem of that? how do i know what might be
thrown?

I'm not sure why C++ doesn't have specifications implemented in this
manner.
Because the compiler is a little prejudiced against enforcing things

across translation units.
what do you call type safety?

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3698
by: Ritz, Bruno | last post by:
hi in java i found that when a method has a throws clause in the definition, callers must either handle the exceptions thrown by the method they are calling or "forward" the exception to the caller by specifying a throws clause as well. is there a similar machanism in c++? i want to force a developer to write handlers for all possible exceptions a method of my class library can throw.
2
2646
by: Dr. StrangeDub | last post by:
I'm looking for a way to force a service to take Dr. Watson dump and terminate from within an asp.net web application (in C#). A bit of background: I am try to code a web/WMI equivalent to an existing C++ utility that we developed to force a Dr. Watson. The C++ version simply uses -- HRESULT result = ControlService( hService, command, &status ); where command is set to 128. I figured I could simply call the...
11
3277
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two "resources" I use are memory and file I/O and whenever there is a memory allocation failure or file I/O failure I just simply call a custom assert-type function to check, print a error message and abort. This seems to be OK for now (for the...
7
2341
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block of code, when System.Exception seems to get the job done for me. My application is an ASP.Net intranet site. When I catch an exception, I log the stack trace and deal with it, normally by displaying an error
15
3274
by: Javier Estrada | last post by:
Can someone explaing the difference between these exception models regarding the structured exception handling? The documentation is not clear. Some code would actually help. Thx
11
5600
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
41
3083
by: Stuart Golodetz | last post by:
Hi all, Just wondering whether there's any reason why exception specifications are enforced at runtime, rather than at compile-time like in Java? (This was prompted by reading an article on GOTW, incidentally.) Is there something about C++ that makes it harder/impossible to check at compile-time? Cheers, Stu
2
3476
by: fischermx | last post by:
Exception Catching difference between VC++ and C# In C# I have this: try { int x, y, z; x = 20; y = 0;
4
4146
by: Terry Olsen | last post by:
I have a class where I throw a new exception from a method My calling code (in a different class) is wrapped in a Try/Catch block, but it's not catching. Instead the IDE is highlighting the ThrowNew Exception line in the class and saying it was unhandled. Looking for some help. Thanks!
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.