Topic: Throwing Exception using A type, Not an expression
Author: stkim@yujinrobot.com ("Kim, Seungtai")
Date: Sat, 13 Nov 2004 01:13:01 GMT Raw View
The current Standard allows only throwing exception using expression.
But, in side of catching it, the handler can catch it using
unmaned paramters. So, any exception can be handled only with
a type information. And the matching process is enough to find an
appropriate exception handler.
So, why can't we throw the exception using a type not an expression?
For example,
try {
...
throw std::logic_error;
}
catch( std::logic_error ) {
...
}
instead of
try {
...
throw std::logic_error ( ) ;
}
catch( std::logic_error ) {
...
}
In many cases, I didn't need the value of the exception. Just
I want to the type of exception, so in many cases, I need not
use the value of exception. Basically, throwing value as an
exception, I think that it cause unnessary value creation process.
Is there an intention of it? Or why dose not it allow?
--
S Kim <stkim@yujinrobot.com>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Sat, 13 Nov 2004 15:24:58 GMT Raw View
""Kim, Seungtai"" <stkim@yujinrobot.com> wrote in message
news:cn3l3t$a67$1@news.kreonet.re.kr...
>
> The current Standard allows only throwing exception using expression.
>
> But, in side of catching it, the handler can catch it using
> unmaned paramters. So, any exception can be handled only with
> a type information. And the matching process is enough to find an
> appropriate exception handler.
>
> So, why can't we throw the exception using a type not an expression?
What happens when the catch expects an object and gets a type?
What happens when the catch expects a type and gets an object?
Since usually the throw site is decoupled from the catch site you have no
general way to pair up the throws and catches.
[snip]
>
> In many cases, I didn't need the value of the exception. Just
> I want to the type of exception, so in many cases, I need not
> use the value of exception. Basically, throwing value as an
> exception, I think that it cause unnessary value creation process.
Not sure what's wrong with this - performance? Declare an empty type
class - will generally be pretty quick to create, or use a pointer.
I think I see what you want but I don't believe the increased complexity
of thew language is worth it.
Roger Orr
--
MVP in C++ at www.brainbench.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Sat, 13 Nov 2004 17:36:36 GMT Raw View
Kim, Seungtai wrote:
> The current Standard allows only throwing exception using expression.
>
> But, in side of catching it, the handler can catch it using
> unmaned paramters. So, any exception can be handled only with
> a type information. And the matching process is enough to find an
> appropriate exception handler.
The matching process looks for a handler that matches the type of the
thrown object. If you don't have the object, what match can you perform?
If you remove the object the process will no longer "be enough" to find
the appropriate handler!
>
> So, why can't we throw the exception using a type not an expression?
>
Because you don't know how it's caught. What would happen if you throw a
type and you catch it by value? Please remember that the throw code and
the catch handlers may reside in completely unrelated portions of code,
for example the catch might be in a third party library which will be
unaware that you're throwing types.
>
> In many cases, I didn't need the value of the exception. Just
> I want to the type of exception, so in many cases, I need not
> use the value of exception. Basically, throwing value as an
> exception, I think that it cause unnessary value creation process.
>
Does it look such a great cost to you? Exception should be thrown in...
exceptional cases. So would it make a big difference?
>
> Is there an intention of it? Or why dose not it allow?
>
I don't think it will ever be allowed, for three reasons:
1) there's no real need for it (of course, you may disagree on this point)
2) such a change would be backward incompatible, because code that
throws a type would no longer interoperate with code that catches an object
3) it would require to rewrite compilers, as throwing an object-less
type is a completely different process than throwing an object.
Alberto
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: house@usq.edu.au (Ron House)
Date: Mon, 15 Nov 2004 17:14:33 GMT Raw View
Kim, Seungtai wrote:
> The current Standard allows only throwing exception using expression.
>
> But, in side of catching it, the handler can catch it using
> unmaned paramters. So, any exception can be handled only with
> a type information. And the matching process is enough to find an
> appropriate exception handler.
>
> So, why can't we throw the exception using a type not an expression?
The implementation has to have something that picks one catcher over
another, and the decision was that the thing holding the necessary
information should be an object. That is simple and obvious, given that
RTTI is available. You propose that C++ compiler writers go to a huge
amount of effort for what? Efficiency? In an uncommonly-executed case?
That's a terrible reason to do anything. The time spent loading your
larger compiler to compile each and every program would probably waste
more time than you ever saved, and that assumes you have some efficient
alternative, which isn't clear in itself, and it assumes that the
creation step of an object _isn't_ efficient.
--
Ron House house@usq.edu.au
http://www.sci.usq.edu.au/staff/house
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Michiel.Salters@logicacmg.com (Michiel Salters)
Date: Mon, 15 Nov 2004 17:23:08 GMT Raw View
stkim@yujinrobot.com ("Kim, Seungtai") wrote in message news:<cn3l3t$a67$1@news.kreonet.re.kr>...
> The current Standard allows only throwing exception using expression.
[...]
> So, why can't we throw the exception using a type not an expression?
[...]
> In many cases, I didn't need the value of the exception. Just
> I want to the type of exception, so in many cases, I need not
> use the value of exception. Basically, throwing value as an
> exception, I think that it cause unnessary value creation process.
[...]
Just throw a null pointer of the specified type. E.g. instead of
throwing the type std::string, throw (std::string*)0
HTH,
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: nagle@animats.com (John Nagle)
Date: Tue, 16 Nov 2004 01:35:50 GMT Raw View
Kim, Seungtai wrote:
> The current Standard allows only throwing exception using expression.
>
> But, in side of catching it, the handler can catch it using
> unmaned paramters. So, any exception can be handled only with
> a type information. And the matching process is enough to find an
> appropriate exception handler.
>
> So, why can't we throw the exception using a type not an expression?
>
> For example,
>
> try {
> ...
> throw std::logic_error;
> }
> catch( std::logic_error ) {
> ...
> }
Just throw an enum value.
namespace nonstd {
enum errors { logic_error, numeric_error, user_error };
};
try {
...
throw nonstd::logic_error;
}
catch (nonstd::errors err) {
switch (err) {
case nonstd::logic_error:
...
}
}
Problem solved.
John Nagle
Animats
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]