exception-handling

does instanceof work for subclassed exceptions?

依然范特西╮ 提交于 2019-12-23 12:53:18
问题 java.net.ConnectException extends java.net.SocketException If I do the following, will it cater for both exceptions? ie if I catch a "parent" exception using instanceof , does that include any subclassed exceptions? catch (Exception e) { if (e instanceof java.net.SocketException) { System.out.println("You've caught a SocketException, OR a ConnectException"); } } (and for the record, yes I know catching plain Exceptions is bad, just using it for this example ;) ) 回答1: Exceptions are regular

ANTLR NoViableAltException with JAVA

北慕城南 提交于 2019-12-23 12:43:08
问题 In my grammar with antlrworks, I can get noviablealtexception for rules like if, while which need corresponding right and left brackets. However, in java, i cannot get noviablealtexception. loop_statement: (WHILE LPAREN expr RPAREN statement) | (DO statement WHILE LPAREN expr RPAREN); condition_statement : IF LPAREN expr RPAREN statement (options {greedy=true;}: ELSE statement)? In statement rule I have a block rule which is, statement_blocks : (LBRACE statement* RBRACE) ; And statement rule

what can't be done with if else clause, and can be done with exception handling?

﹥>﹥吖頭↗ 提交于 2019-12-23 12:32:52
问题 what can't be done with if else clause, and can be done with exception handling ? In other words where do we actually need to use exception handling and mere if else won't serve the purpose. Is exception handling just a glorified way of showing errors ? 回答1: In the earliest C++ implementations, exceptions were compiled into equivalent if/else constructs, more or less. And they were slow as molasses, to the point where you can still find programming guides that recommend against exceptions on

Testing for Exceptions using JUnit. Test fails even if the Exception is caught

北城以北 提交于 2019-12-23 12:27:25
问题 I am new to testing with JUnit and I need a hint on testing Exceptions. I have a simple method that throws an exception if it gets an empty input string: public SumarniVzorec( String sumarniVzorec) throws IOException { if (sumarniVzorec == "") { IOException emptyString = new IOException("The input string is empty"); throw emptyString; } I want to test that the exception is actually thrown if the argument is an empty string. For that, I use following code: @Test(expected=IOException.class)

May I modify the value of an exception inside a std::exception_ptr?

时间秒杀一切 提交于 2019-12-23 11:52:33
问题 If I have a exception stored inside std::exception_ptr . I rethrow the exception using std::rethrow_exception , access it using catch(MyException&) and then I modify the value. If I throw the same exception again, should I observe the modification I made? The following code demonstrate my idea: #include <exception> #include <iostream> struct MyException { int value; }; int main() { std::exception_ptr a = std::make_exception_ptr(MyException()); try { std::rethrow_exception(a); } catch

May I modify the value of an exception inside a std::exception_ptr?

ε祈祈猫儿з 提交于 2019-12-23 11:52:08
问题 If I have a exception stored inside std::exception_ptr . I rethrow the exception using std::rethrow_exception , access it using catch(MyException&) and then I modify the value. If I throw the same exception again, should I observe the modification I made? The following code demonstrate my idea: #include <exception> #include <iostream> struct MyException { int value; }; int main() { std::exception_ptr a = std::make_exception_ptr(MyException()); try { std::rethrow_exception(a); } catch

Error: The conversion of a nvarchar data type to a smalldatetime data type resulted in an out-of-range value

时光毁灭记忆、已成空白 提交于 2019-12-23 11:20:30
问题 Hey all I'm trying to do the following insert query SqlDataSource userQuizDataSource = new SqlDataSource(); userQuizDataSource.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=quizApp;Integrated Security=True"; userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([DateTimeComplete], [Score], [UserName]) VALUES (@DateTimeComplete, @Score, @UserName)"; userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString()); userQuizDataSource

Servlet/filter specific exception handling in java

◇◆丶佛笑我妖孽 提交于 2019-12-23 10:48:06
问题 I have a servlet extending HttpServlet and implementing a GET request. I also use a filter (from an external library) which is mapped to the above servlet url. Now an exception is thrown by the filter, and as expected I get this SEVERE: Servlet.service() for servlet [myServlet] in context with path [] threw exception I know error-page description is probably a standard way to catch this exception, but is there a way to catch the exception from a specific servlet filter ? I already have an

StackOverflowException was unhandled

痴心易碎 提交于 2019-12-23 10:28:05
问题 I'm having this error in my code An unhandled exception of type 'System.StackOverflowException' occurred in MedCareProviderLibrary.dll Here is a snippet of my code and where the error is coming from. It gives a yellow arrow on the part with the error. The part showing the error is in bold. Any help will be much appreciated Thanks private string _TestNo; private string _TestType; private DateTime _TestDate; private string _PatientNo; private string _DoctorNo; public Test() { _TestNo = "";

Distinguish between exception of same type

感情迁移 提交于 2019-12-23 10:07:17
问题 What is the best method to distinguish between two exception of the same type but thrown for different reasons. For example InvalidOperationException can be thrown attempting to access an empty sequence, but it can also be thrown when using the Concurrent object (e.g. BlockingCollection ) From my reading I've taken the conclusion that using the BlockingCollection.CompleteAdding() to signal completion is perfectly fine and as such one would need to catch the Exception and handle appropriately