catch

Scala: How to declare a variable (var) before using it in try/catch/finally alvin June 7, 2015 - 4:04pm

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.”

Problem

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.

How to match one or more exceptions with try/catch in Scala

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.”

Problem

You want to catch one or more exceptions in a try/catch block.

Solution

The Scala try/catch/finally syntax is similar to Java, but it uses the match expression approach in the catch block:

Scala control structure examples (if/then, match/case, for, while, try/catch) alvin September 28, 2014 - 6:43pm

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:

A Scala Either, Left, and Right example (like Option, Some, and None) alvin October 4, 2012 - 4:38pm
Scala: A discussion of the Option/Some/None pattern, and how to use Either/Left/Right instead, when you need to know why something failed.

Handling Drupal SQL exceptions (db_insert, db_update, db_delete)

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:

How to create and throw a custom exception in Java

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.

What is a Java NumberFormatException? alvin November 29, 2007 - 5:37pm

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 NumberFormatException: