-2

So I have code in Java, now I want that in C++. Everything is fine but I'm having trouble with exceptions.

In the 'Test' class in Java I have

try {
    k = Integer.parseInt(args[i]);
}
catch (OutOfRangeException ex) {
    System.out.println(args[i]+ ex.getMessage());               
}

This is 'OutOfRange.java'

public class OutOfRangeException extends Exception {
    public OutOfRangeException (String message) {
        super (message);
    }
}

And method from another class that throws that exception

public int number (int m) throws OutOfRangeException {
        if (m < 0 || m >= arr.length) {
            throw new OutOfRangeException(" - number out of range");
        }
        return arr[m];
    }
};

I can't get the same effect with C++, I read a lot about it but still it doesn't work (I get 'dynamic exception specifications are deprecated in C++11' when it comes to 'throw' but I don't know how to get message in C++ either).

Is there a way I can do this like in Java?

I tried something like this: In main:

try {
    k = stoi(argv[i]);
}
catch (OutOfRangeException &e) {
    cout << argv[i]<< endl;
}

Method:

int PrimeNumbers: number (int m) throw (OutOfRangeException) {
    if (m < 0 || m >= sizeof(arr)) {
        throw  OutOfRangeException(" - number out of range");
    }
    return arr[m];
}

and constructor of OutOfRangeException

OutOfRangeException::OutOfRangeException(string message) {
    cout<<message;
}

The thing is I get this 'dynamic exception...' error and I haven't found any other way to do it so I would be satisfied with it.

Nerwena
  • 23
  • 6
  • Please show what you tried in a [repro]. Also you need to learn the language from a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You cannot translate Java to C++ like that. Many things that *look* the same in the two languages have completely different semantics. – walnut Mar 07 '20 at 19:34
  • 3
    C++ is not Java. Exceptions in C++ are similar to, but are also fundamentally different, in several key ways, from exceptions in Java. As your compiler diagnostic informs you, Java-like exception specifications are deprecated in C++11. It's not just exceptions, but you simply can't assume that things that are similarly called in Java and C++ work the same way. If you keep assuming this, as you learn C++, you will be constantly confused. It will actually be more productive if you completely forget everything you know about Java, while learning C++. – Sam Varshavchik Mar 07 '20 at 19:36
  • I agree but my lecturer used 'throw' and everything worked fine... I know that these are diffrent languages, this code I added is very similar to Java because I didn't know what to do. – Nerwena Mar 07 '20 at 19:42
  • I've searched the web many times to find solution, something similar but I keep doing something wrong because it doesn't want to work so I need more clarification. – Nerwena Mar 07 '20 at 19:44
  • You may want to read about [`std::stoi`](https://en.cppreference.com/w/cpp/string/basic_string/stol). – Eljay Mar 07 '20 at 19:44
  • As I understand stoi has exception out_of_range if it is our of range of its type. But I want to throw exception of another range... – Nerwena Mar 07 '20 at 19:48
  • C++ is not Java. The best way to learn C++ is with a good book. "Searching the web" will not be very productive. – Sam Varshavchik Mar 07 '20 at 20:13

1 Answers1

1

Dynamic exceptions specifications like

throw (OutOfRangeException)

in a function declaration have been deprecated since C++11 and were removed from C++ with C++17. Functions in modern C++ have only two exception specifications: Either can throw or cannot throw. The default is the former, so you don't need this specification at all.


int PrimeNumbers: number (int m) makes no sense, did you mean int PrimeNumbers::number (int m)?


sizeof(arr) does not do what you think it does. It returns the size of an object in bytes. I don't know how arr is declared, but it is almost surely wrong.


std::stoi throws the exception std::out_of_range if the parsed value does not fit into the type. It never throws your custom type OutOfRangeException.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • Thank you, as I said when my lecturer talked about exceptions he had throw in declaration so I'm a little confused but now I know what the right way is. Well, again I was taught that when I define method which return int I should write it like that. Thanks, I fixed the size. – Nerwena Mar 07 '20 at 20:04
  • @Nerwena `int PrimeNumbers: number (int m)` is not valid C++ at all and never was. `throw(...)` in a function declaration was correct 10+ years ago, but since C++11 it should be `noexcept(false)` and `noexcept(true)` instead (where the first is default). – walnut Mar 07 '20 at 20:07
  • Thank you, I fiddled with my code and now it works fine. I will take into account your tips, you really helped me :) Looks like my lectures are very outdated... – Nerwena Mar 07 '20 at 20:14