|
-
Dec 29th, 2001, 01:41 AM
#1
Thread Starter
Addicted Member
newbie..Exception Handling
here's a code I gleaned frrom a book...
#include<iostream.h>
class Bummer{};
class Dummer{};
class Dumm{};
void main()
{
try{
throw Dumm();
}
catch(Bummer)
{
cout<<"helo Bumer";
}
catch(Dummer)
{
cout<<"helo Dummer";
}
catch(...)
{
cout<<"helo rest of u";
}
}
what I wanna know is tt y is it necessary 2 create these Classes ..Bummer Dummer..I mean..y cant v just say sumthing lik
throw(5)
catch(int i)
{
select case i
{
//coding here...
}
-
Dec 29th, 2001, 06:55 AM
#2
transcendental analytic
It's not nessesary, you can use it for simple error codes, but if you want to have an error container that stores additional data about what happened, say if you have an error class filenotfound, then you can store it's filename and in the catch you display the filename that wasn't found.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 29th, 2001, 10:34 AM
#3
Monday Morning Lunatic
Anything can be thrown, but the best thing to throw is a class derived from the standard library's exception class. For example:
Code:
#include <iostream>
#include <stdexcept>
using std::cout;
using std::endl;
int main() {
try {
throw std::logic_error("Exception");
throw std::bad_alloc;
} catch(std::exception &ex) {
cout << ex.what() << endl;
}
return 0;
}
I passed a reference in the exception handler because that means you can catch anything derived from std::exception, and still be able to get the proper information off it.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 29th, 2001, 11:41 AM
#4
transcendental analytic
what if you made something like this:
template <const char* T>class Exception{};
...
try{
throw Exception<"File not found">();
}
catch(Exception<"File not found">){
}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 30th, 2001, 07:29 AM
#5
Monday Morning Lunatic
That won't work unless the compiler optimises the two strings down to exactly the same pointer (since they're the same string - it's the "Eliminate Duplicate Strings" option in MSVC).
And how do you catch any kind of Exception object?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 30th, 2001, 07:31 AM
#6
transcendental analytic
just regular inheritance, btw it didn't work :s
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 30th, 2001, 07:52 AM
#7
Monday Morning Lunatic
Didn't think it would - anyway, for exception handling virtual inheritance is perfectly okay. The performance aspect goes pretty much out of the window in an exceptional case.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|