try-catch

Java ConcurrentHashMap not thread safe.. wth?

风流意气都作罢 提交于 2019-12-10 03:19:17
问题 I was using HashMap before like public Map<SocketChannel, UserProfile> clients = new HashMap<SocketChannel, UserProfile>(); now I've switched to ConcurrentHashMap to avoid synchronized blocks and now i'm experiencing problems my server is heavily loaded with 200-400 concurrent clients every second which is expected to grow over time. which now looks like this public ConcurrentHashMap<SocketChannel, UserProfile> clients = new ConcurrentHashMap<SocketChannel, UserProfile>(); My server design

JSLint complaining about my try/catch

孤街醉人 提交于 2019-12-10 03:11:17
问题 The javascript, when run through JSLint yells at me and I am not sure why. /*jslint browser: true, devel: true, evil: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, newcap: true, immed: true */ var foo = function() { try { console.log('foo'); } catch(e) { alert(e); } try { console.log('bar'); } catch(e) { alert(e); } }; foo(); It tells me: Problem at line 12 character 11: 'e' is already defined. } catch(e) { It appears to be upset that I have a second catch(e) .

error: cannot dynamic_cast … (target is not pointer or reference)

拥有回忆 提交于 2019-12-10 01:54:49
问题 I'm learning exception handling in C++ and run into a problem. Here's the code: #include<iostream> #include<exception> using namespace std; class A { public: virtual void f(void){} }; class AA:public A { public: void aa(void){}; }; int main(void) { A a; try { dynamic_cast<AA>(a).aa(); } catch(exception ex) { cout<<"["<<ex.what()<<"]"<<endl; } return 0; } So I thought the try catch will allow the function to execute and show me the content of the exception, but my compiler does not compile it.

javascript - catch SyntaxError and run alternate function

本秂侑毒 提交于 2019-12-10 01:14:01
问题 I'm trying to build something on javascript that I can have an input that can be everything like string, xml, javascript and (non-javascript string without quotes) as follows: //strings eval("'hello I am a string'"); /* note the following proper quote marks */ //xml eval(<p>Hello I am a XML doc</p>); //javascript eval("var hello = 2+2;"); So this first 3 are working well since they are simple javascript native formats but when I try use this inside javascript //plain-text without quotes eval(

How to use try-catch block for PDO

╄→尐↘猪︶ㄣ 提交于 2019-12-09 19:12:31
问题 What is a decent way to handle PDO error when using try catch block? Currently I have something like this: BlogModel.php class BlogModel extends Model { public function save($id, $value) { $stmt = $this->getDb()->prepare('UPDATE setting SET name = :name WHERE id = :id'); $stmt->bindParam(':id', $id); $stmt->bindParam(':name', $values); return ($stmt->execute() !== false) ? $id : false; } } So, in the controller BlogController.php , I would do something like this: <?php class Blog extends

Exception handling in Linq queries

£可爱£侵袭症+ 提交于 2019-12-09 18:58:35
问题 I m using ... tmpLst = (from e in App.lstAllChilds where e.Id == Id select e).ToList(); where lstAllChilds is the list, which contains some corrupted data as well. So now i m tying to handle Try-Catch block inside this query. Please help. 回答1: Here is the simplest change you can make to simply exclude items which are null or are raising an exception. tmpLst = App.lstAllChilds.Where(e => { try { return e != null && e.Id == Id; } catch { return false; } }).ToList(); But in my opinion you

How could I catch an “Unicode non-character”-warning?

倾然丶 夕夏残阳落幕 提交于 2019-12-09 17:53:32
问题 How could I catch the "Unicode non-character 0xffff is illegal for interchange"-warning? #!/usr/bin/env perl use warnings; use 5.012; use Try::Tiny; use warnings FATAL => qw(all); my $character; try { $character = "\x{ffff}"; } catch { die "---------- caught error ----------\n"; }; say "something"; Output: # Unicode non-character 0xffff is illegal for interchange at ./perl1.pl line 11. 回答1: A Perl 5.10.0 ⋯ 5.13.8 Bug I’m going to assume that you don’t actually want to “catch” this warning,

Throwing an UnsupportedOperationException

佐手、 提交于 2019-12-09 16:39:39
问题 So one of the method descriptions goes as follows: public BasicLinkedList addToFront(T data) This operation is invalid for a sorted list. An UnsupportedOperationException will be generated using the message "Invalid operation for sorted list." My code goes something like this: public BasicLinkedList<T> addToFront(T data) { try { throw new UnsupportedOperationException("Invalid operation for sorted list."); } catch (java.lang.UnsupportedOperationException e) { System.out.println("Invalid

JavaScript: define a constant inside try / catch with strict mode

走远了吗. 提交于 2019-12-09 16:06:50
问题 Today I run into a weird JS bug, working with const inside a try/catch block, and I'd like to better understand what is causing it. Let's look at a code example, that is worth more than a thousand words: try { const FOO = 'bar'; console.log('inside:', FOO); } catch (e) {} console.log('outside:', FOO); This will log: inside: bar outside: bar If we switch to "strict mode" though: 'use strict'; try { const FOO = 'bar'; console.log('inside:', FOO); } catch (e) {} console.log('outside:', FOO); Now

Quick failure when using for-comprehension with scala.util.Try

落爺英雄遲暮 提交于 2019-12-09 16:02:30
问题 I really like scala.util.Try in Scala 2.10, and how it works with for-comprehension makes handling multiple steps that could go wrong easily. For example, we could use the following code to make sure we only print out that two numbers if and only if everything is under control and we get value correctly. def tryA: Try[Int] = {....} def tryB: Try[Int] = {....} for { a <- tryA b <- tryB } { println (s"We got:${a+b}") } But one of my concern is that this code is actually ignore any exceptions,