Catching multiple exceptions at once in Scala

You can bind the whole pattern to a variable like this: try { throw new java.io.IOException(“no such file”) } catch { // prints out “java.io.IOException: no such file” case e @ (_ : RuntimeException | _ : java.io.IOException) => println(e) } See the Scala Language Specification page 118 paragraph 8.1.11 called Pattern alternatives. Watch Pattern … Read more

Check whether a string is parsable into Long without try-catch?

You can create rather complex regular expression but it isn’t worth that. Using exceptions here is absolutely normal. It’s natural exceptional situation: you assume that there is an integer in the string but indeed there is something else. Exception should be thrown and handled properly. If you look inside parseLong code, you’ll see that there … Read more

Using try/catch for preventing app from crashes

Of course, there are always exceptions to rules, but if you need a rule of thumb – then you are correct; empty catch blocks are “absolutely” bad practice. Let’s have a closer look, first starting with your specific example: try { View view = findViewById(R.id.toolbar); } catch(Exception e) { } So, a reference to something … Read more

Is having a return statement just to satisfy syntax bad practice?

A clearer way without an extra return statement is as follows. I wouldn’t catch CloneNotSupportedException either, but let it go to the caller. if (a != null) { try { return a.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } throw new TotallyFooException(); It’s almost always possible to fiddle with the order to end up … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)