This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.17, “How to declare a variable (var) before using it in try/catch/finally.”
You want to use an object in a
try block, and need to access it in the
finally portion of the block, such as when you need to call a
close method on an object.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.16, “How to match one or more exceptions with try/catch in Scala.”
You want to catch one or more exceptions in a try/catch block.
The Scala try/catch/finally syntax is similar to Java, but it uses the
match expression approach in the
This post contains a collection of Scala control structures examples. I initially created most of these in the process of writing the Scala Cookbook. Unlike the Cookbook, I don’t describe them much here, I just show the examples, mostly as a reference for myself (and anyone else that can benefit from them).
if/then control structures:
Here are some examples of the Scala if/then control structure:
Sometimes when I write small Scala scripts and programs I loosen the reins and use a
var. When you do this and you may need to occasionally create a null variable (a var, not a val), such as when you need to declare a variable right before using it in a try, catch, finally block.
Scala FAQ: Can you share some examples of the Scala try/catch/finally syntax? Also, can you show how to catch multiple exceptions, and just one exception with the Scala wildcard operator (_)?
The general Scala try/catch/finally syntax looks like this:
While developing a Drupal module, I just ran into a situation where it may be common for users to generate SQL exceptions. I have a 'unique' limit on several of my database tables, essentially saying that certain name fields must be unique for the current project. As you can imagine, it's extremely easy to enter a duplicate name, and while doing a SQL INSERT that can easily lead to a SQL exception.
As a result, I dug around and found a way to handle SQL exceptions in Drupal queries. My current problem is with a Drupal 7 db_insert query, and I handled it like this:
Java exceptions FAQ: How do I create a custom exception in Java?
Here's a quick example of how to create and throw a custom exception class in Java. In this tutorial I'll demonstrate how to (1) create a custom exception class in Java; (2) throw our custom Java exception; (3) catch our custom exception; and (4) look at the output from our custom exception when we print a stack trace.
Java exception FAQ: What is a Java NumberFormatException?
Answer: A Java
NumberFormatException usually occurs when you try to do something like convert a
String to a numeric value, like an int, float, double, long, etc.
The best way to show a NumberFormatException is by example, so here’s an example where I intentionally write bad Java code to throw a