exception-handling

Correct Exceptions in C++

China☆狼群 提交于 2021-02-06 14:49:04
问题 I am just learning how to handle errors in my C++ code. I wrote this example that looks for a text file called some file, and if its not found will throw an exception. #include <iostream> #include <fstream> using namespace std; int main() { int array[90]; try { ifstream file; file.open("somefile.txt"); if(!file.good()) throw 56; } catch(int e) { cout<<"Error number "<<e<<endl; } return 0; } Now I have two questions. First I would like to know if I am using Exceptions correctly. Second,

Correct Exceptions in C++

冷暖自知 提交于 2021-02-06 14:48:10
问题 I am just learning how to handle errors in my C++ code. I wrote this example that looks for a text file called some file, and if its not found will throw an exception. #include <iostream> #include <fstream> using namespace std; int main() { int array[90]; try { ifstream file; file.open("somefile.txt"); if(!file.good()) throw 56; } catch(int e) { cout<<"Error number "<<e<<endl; } return 0; } Now I have two questions. First I would like to know if I am using Exceptions correctly. Second,

What exceptions does Newtonsoft.Json.DeserializeObject throw?

强颜欢笑 提交于 2021-02-05 12:52:10
问题 What exceptions does Newtonsoft.Json.DeserializeObject throw? I want to handle them. http://james.newtonking.com/json/help/?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObject.htm#seeAlsoToggle 回答1: JSON.NET defines the following exceptions: JsonException JsonReaderException JsonSerializationException JsonWriterException JsonSchemaException Serialization or deserialization errors will typically result in a JsonSerializationException . 回答2: Note that Json.NET's error handling

What exceptions does Newtonsoft.Json.DeserializeObject throw?

北城余情 提交于 2021-02-05 12:51:22
问题 What exceptions does Newtonsoft.Json.DeserializeObject throw? I want to handle them. http://james.newtonking.com/json/help/?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObject.htm#seeAlsoToggle 回答1: JSON.NET defines the following exceptions: JsonException JsonReaderException JsonSerializationException JsonWriterException JsonSchemaException Serialization or deserialization errors will typically result in a JsonSerializationException . 回答2: Note that Json.NET's error handling

Why throwing a general Exception in method is bad? [duplicate]

余生颓废 提交于 2021-02-05 12:17:21
问题 This question already has answers here : Throwing generic Exception discouraged? (5 answers) Closed 4 years ago . Why throwing by a method a general Exception is a bad practice in Java? class Test{ public void ex() throws Exception{ //...some code throwing for eg. IllegalAccesException() } } 回答1: When you throw an exception by a method, you should normally know what kind of exception it is. if you throw a general exception rather a specific exception, you will loose the specific detail of the

C++, catch user-defined exceptions in multiple blocks

柔情痞子 提交于 2021-02-05 11:10:54
问题 Suppose the following example. There are classes A-C derived from std::exception: #include <exception> #include <string> #include <iostream> class A : public std::exception { std::string a_text; public: A(const std::string & a_text_) : a_text(a_text_) {} virtual ~A() throw() { } }; class B : public A { const std::string b_text; public: B(const std::string &a_text_, const std::string & b_text_) : A(a_text_), b_text(b_text_) {} virtual ~B() throw() {} }; template <typename T> class C : public B

C++, catch user-defined exceptions in multiple blocks

若如初见. 提交于 2021-02-05 11:10:27
问题 Suppose the following example. There are classes A-C derived from std::exception: #include <exception> #include <string> #include <iostream> class A : public std::exception { std::string a_text; public: A(const std::string & a_text_) : a_text(a_text_) {} virtual ~A() throw() { } }; class B : public A { const std::string b_text; public: B(const std::string &a_text_, const std::string & b_text_) : A(a_text_), b_text(b_text_) {} virtual ~B() throw() {} }; template <typename T> class C : public B

C++ read/write to binary file. on program exit it gives System.AccessViolationException

江枫思渺然 提交于 2021-02-05 09:20:44
问题 This C++ program is created using Visual Studio 2010. It's a group project that has everyone in class stumped. The program initially starts fine and the user can run through the program and add items that are written out to file. the items are read back in and displayed. When the user is done, on the program exiting return 0; it gives me "An unhandled exception of type System.AccessViolationException occurred. Attempted to read or write protected memory. This is often an indication that other

How can I tell Visual Studio to NOT BREAK on a particular exception?

a 夏天 提交于 2021-02-04 22:14:24
问题 I have a particular type of exception that I would like Visual Studio to not break on and show the Exception Assistant screen. Essentially I would like it just to let my normal exception handling infrastructure deal with it. The exception is an inheritor of System.Exception which I wrote and have the source code for. Any where this is thrown I want VS to not catch it, ie it is not useful to just supress a single throw new BlahException(); in code. This is because the exception is thrown a lot

How can I tell Visual Studio to NOT BREAK on a particular exception?

懵懂的女人 提交于 2021-02-04 22:12:40
问题 I have a particular type of exception that I would like Visual Studio to not break on and show the Exception Assistant screen. Essentially I would like it just to let my normal exception handling infrastructure deal with it. The exception is an inheritor of System.Exception which I wrote and have the source code for. Any where this is thrown I want VS to not catch it, ie it is not useful to just supress a single throw new BlahException(); in code. This is because the exception is thrown a lot