Posts

Showing posts with the label try-catch

while/try/except decimal TypeError not working

while/try/except decimal TypeError not working I thought I could finally get some use out of the while/try/except loop, but I am encountering a problem. My task is to have the computer request numbers from a user until '0' is entered. After this, the numbers entered will be added. My problem is that the Except part isn't catching non-numeric entries. If a non-numeric entry is entered, I get a regular error message that stops the program. Here is what happens when I enter 3 numbers and the 4th entry is 'm': on win32 Type "copyright", "credits" or "license()" for more information. >>> RESTART: C:/Users/Username/Documents/CISPROG1/Homework 5/Homework5.1.SumGenerator.py Enter 1st element (or type '0' to finish). 1 Enter 2nd element (or type '0' to finish). 2 Enter 3rd element (or type '0' to finish). 3 Enter 4th element (or type '0' to finish). m Traceback (most recent call last): File "C:/...

I need to check if thrown exception from operator=?

I need to check if thrown exception from operator=? Given the following code: template <class T> class A { T* arr; int size; public: A(int size) : arr(new T[size]) , size(size) { } //.. A& operator=(const A& a){ if(this == &a){ return *this; } this->size = a.size; T* ar=new T[a.size]; for(int i=0 ; i<size ; i++){ ar[i]=a.arr[i]; // I need to do it at "try-catch" ? } delete this->arr; this->arr=ar; return *this; } //... }; When I copy the elements from the given array, do I need to do it with a try-catch clause or not? is it a good idea or not? try-catch Aren't there some guarantees about "assignment [should] never throw(s)" in [a valid program in] C++? – user2864740 Jun 30 at 20:18 ...