throw

C++: Throwing a derived class by reference does not work when catching base class

拈花ヽ惹草 提交于 2019-11-29 06:12:47
I want to throw my own exceptions with the base class Exception . There is a virtual method print which will be overwritten by the subclasses. I only catch the type Exception& and use print to get the specific error. The problem is that once I throw a reference of a subclass it is trated as if it were the base class. Here is an example: #include <iostream> using namespace std; class Exception { public: virtual void print() { cout << "Exception" << endl; } }; class IllegalArgumentException : public Exception { public: virtual void print() { cout << "IllegalArgumentException" << endl; } }; int

Why is 'throws' not type safe in Swift?

可紊 提交于 2019-11-29 02:05:54
问题 The biggest misunderstanding for me in Swift is the throws keyword. Consider the following piece of code: func myUsefulFunction() throws We cannot really understand what kind of error it will throw. The only thing we know is that it might throw some error. The only way to understand what the error might be is by looking at the documentation or checking the error at runtime. But isn't this against Swift's nature? Swift has powerful generics and a type system to make the code expressive, yet it

Does throw inside a catch ellipsis (…) rethrow the original error in C++?

一笑奈何 提交于 2019-11-29 01:03:10
If in my code I have the following snippet: try { doSomething(); } catch (...) { doSomethingElse(); throw; } Will the throw rethrow the specific exception caught by the default ellipsis handler? Yes. The exception is active until it's caught, where it becomes inactive. But it lives until the scope of the handler ends . From the standard, emphasis mine: §15.1/4: The memory for the temporary copy of the exception being thrown is allocated in an unspecified way, except as noted in 3.7.4.1. The temporary persists as long as there is a handler being executed for that exception. That is: catch(...)

continuing execution after an exception is thrown in java

北战南征 提交于 2019-11-29 00:47:03
问题 I'm trying to throw an exception (without using a try catch block) and my program finishes right after the exception is thrown. Is there a way that after I throw the exception, to then continue execution of my program? I throw the InvalidEmployeeTypeException which I've defined in another class but I'd like the program to continue after this is thrown. private void getData() throws InvalidEmployeeTypeException{ System.out.println("Enter filename: "); Scanner prompt = new Scanner(System.in);

Finally in C++

落花浮王杯 提交于 2019-11-28 23:10:26
问题 Is this a good way to implement a Finally-like behavior in standard C++? (Without special pointers) class Exception : public Exception { public: virtual bool isException() { return true; } }; class NoException : public Exception { public: bool isException() { return false; } }; Object *myObject = 0; try { // OBJECT CREATION AND PROCESSING try { myObject = new Object(); // Do something with myObject. } // EXCEPTION HANDLING catch (Exception &e) { // When there is an excepion, handle or throw,

How to throw good exceptions?

好久不见. 提交于 2019-11-28 21:31:55
I heard you should never throw a string because there is a lack of information and you'll catch exceptions you dont expect to catch. What are good practice for throwing exceptions? do you inherit a base exception class? Do you have many exceptions or few? do you do MyExceptionClass& or const MyExceptionClass& ? etc. Also i know exceptions should never been thrown in destructors i'll add that i understand design by contract and when to throw exception. I am asking how i should throw exceptions. In my opinion, a function should throw an exception if it can't keep its "promise", if it has to

Forwarding an error in Swift

♀尐吖头ヾ 提交于 2019-11-28 14:46:49
Is there a better solution to forward a Swift error from one function to another? In the moment, I'm doing it like this: enum Error:ErrorType{ case Error1 case Error2 } func func1()throws{ do{ try func2() }catch Error.Error1{ throw Error.Error1 }catch Error.Error2{ throw Error.Error2 } } func func2()throws{ // proof something throw Error.Error1 } So, to forward an error, I need to catch all the errors and throw them again. Is there a better solution? Yes: don't wrap it in a do ... catch block. func func2() throws{ // proof something throw Error.Error1 } func func1()throws{ try func2() } You

Boost symbol not found

巧了我就是萌 提交于 2019-11-28 12:14:51
I'm trying to compile/port an older version of OpenOffice. It uses Boost v1.34.1, which is part of the source tree. The error message is as follows: Undefined symbols: "boost::throw_exception(std::exception const&)", referenced from: boost::detail::shared_count::shared_count<ScToken>(ScToken*)in detfunc.o ld: symbol(s) not found Boost is new to me, and I haven't been able to find much online to help me understand this. From the error message, I understand that I probably need to link a library. However, boost::throw_exception is defined in a header file with no matching library (that I can

What is the difference between throw and throw with arg of caught exception?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 08:13:57
Imagine two similar pieces of code: try { [...] } catch (myErr &err) { err.append("More info added to error..."); throw err; } and try { [...] } catch (myErr &err) { err.append("More info added to error..."); throw; } Are these effectively the same or do they differ in some subtle way? For example, does the first one cause a copy constructor to be run whereas perhaps the second reuses the same object to rethrow it? Depending on how you have arranged your exception hierarchy, re-throwing an exception by naming the exception variable in the throw statement may slice the original exception object

How to throw an error in MySql procedure?

☆樱花仙子☆ 提交于 2019-11-28 07:13:06
问题 What is the mechanism to force the MySQL to throw an error within the stored procedure? I have a procedure which call s another function: PREPARE my_cmd FROM @jobcommand; EXECUTE my_cmd; DEALLOCATE PREPARE my_cmd; the job command is: jobq.exec("Select 1;wfdlk# to simulatte an error"); then: CREATE PROCEDURE jobq.`exec`(jobID VARCHAR(128),cmd TEXT) BEGIN DECLARE result INT DEFAULT 0; SELECT sys_exec( CONCAT('echo ',cmd,' | base64 -d > ', '/tmp/jobq.',jobID,'.sh ; bash /tmp/jobq.',jobID,'.sh &>