The correct way to initialize an object using the throw throw constructor

This seems to be a trivial question, but I hung it for a few hours (maybe too much Java killed my C ++ braincells).

I created a class that has the following constructor (i.e. the default constructor)

VACaptureSource::VACaptureSource( std::string inputType, std::string inputLocation ) {
    if( type == "" || location == "" ) {
    throw std::invalid_argument("Empty type or location in VACaptureSource()");
}
type = inputType;
location = inputLocation;

// Open the given media source using the appropriate OpenCV function.
if( type.compare("image") ) {
    frame = cvLoadImage( location.c_str() );
    if( !frame ) {
        throw std::runtime_error("error opening file");
    }
}
else {
    throw std::invalid_argument("Unknown input type in VACaptureSource()");
}

}

When I want to create an instance, I use

    // Create input data object
try {
    VACaptureSource input = VACaptureSource("image", "/home/cuneyt/workspace/testmedia/face_images/jhumpa_1.jpg");
}
catch( invalid_argument& ia ) {
    cerr << "FD Error: " << ia.what() << endl;
    usage(argv[0]);
}
catch( runtime_error& re ) {
    cerr << "FD Error: " << re.what() << endl;
    usage(argv[0]);
}

However, in this case, the instance is local to this block, and I cannot reference it anywhere. On the other hand, I can’t say

VACAptureSource input;

at the beginning of the program, since there is no default constructor.

What is the right way to do this?

Thanks!

+5
source share
9 answers

How to use a pointer (or some version of RAII)?

VACaptureSource* input = NULL;

try {
    input = new VACaptureSource(...);
} catch(...) {
    //error handling
}

//And, of course, at the end of the program
delete input;
+8

try?

try {
  VACaptureSource input = VACaptureSource("image", "/home/cuneyt/workspace/testmedia/face_images/jhumpa_1.jpg");
}
//catch....

//do stuff with input

try:

try {
  VACaptureSource input = VACaptureSource("image", "/home/cuneyt/workspace/testmedia/face_images/jhumpa_1.jpg");
  //do stuff with input
}
//catch....

, try:

void doStuff(VACaptureSource& input){
  //do stuff with input
}

try {
  VACaptureSource input = VACaptureSource("image", "/home/cuneyt/workspace/testmedia/face_images/jhumpa_1.jpg");
  doStuff(input);
}
//catch....

, , , .

+11

, (, Java), , ( Java), , , try ( , ) - (, ) .

+4

, , . "" , , . RAII , .

+3

VACaptureSource* input;
// Create input data object
try {
    input = new VACaptureSource("image", "/home/cuneyt/workspace/testmedia/face_images/jhumpa_1.jpg");
}
catch( invalid_argument& ia ) {
    cerr << "FD Error: " << ia.what() << endl;
    usage(argv[0]);
}
catch( runtime_error& re ) {
    cerr << "FD Error: " << re.what() << endl;
    usage(argv[0]);
}

,

delete input
+2

VACaptureSource input;

, .

, : , VACaptureSource . . , VACaptureSource try .

+2

, ? create() .

:

VACaptureSource input;
try
{
   input.create("image", "...");
}
catch(...)
{
   ...
}

, . , create(), - ...

+1

:

, :

  • const.


  • . .
  • const .
  • , .

, ( ).

VACaptureSource::VACaptureSource( std::string const& inputType,
                                  std::string const& inputLocation )
      :type(inputType)
      ,location(inputLocation)
{
    // Other Code that throws.
}
void playWithCode()
{
    // Get input information from user.
    VACaptureSource input("image", "/home/cuneyt/workspace/testmedia/face_images/jhumpa_1.jpg");

    // use the input object.
    // Do not need to play with pointers here.
}
int main()
{
    try
    {
        playWithCode();
    }
    catch( invalid_argument const& ia )
    {    cerr << "FD Error: " << ia.what() << endl;
         usage(argv[0]);
    }
    catch( runtime_error const& re )
    {    cerr << "FD Error: " << re.what() << endl;
         usage(argv[0]);
    }
}
+1

Simple. . , try, , ( , ? )

UPDATE0 : Although I'm not sure if memory management is an issue if you are using an instance.

UPDATE1 : Hmm, maybe I'm thinking of exceptions in destructors.

int
main2(int argc, char* argv[])
{
  MyClass class;
  class.doSomething();
}

int
main(int argc, char* argv[])
{
  int result = 0;
    try {
      main2(argc, argv);
    } catch (std::exception& se) {
      // oh noes!
       result = 1;
    }
  return result;
}
0
source

All Articles