With a title like that, you know it's time once again visit the wonderful world of exceptions. Our first (yes, first; it's a two-for-one day!) example is from production code that Valdas found which demonstrates a very perverse (yet intriguing) way of abusing not only try/catch blocks, but regular exceptions as well ...
public boolean quote_string_checker (String stringtoscan) { boolean testval = true; try { Pattern p = Pattern.compile("[a-z]"); for(int i=0;i<stringtoscan.length();i++) { char chartoscan=stringtoscan.charAt(i); //Matcher m = p.matcher(chartoscan); //String s=""+chartoscan; String c=Character.toString(chartoscan); Matcher m=p.matcher(c); testval = m.matches(); if (testval==false) break; } } catch (java.util.regex.PatternSyntaxException e) { testval = false; } return testval; }
And our second example is from Dan Rahn, whose colleague shows us how easy it is to simulate everyone's favorite On Error Resume Next using C# (instead, of course, checking for .IsNull) ...
try { try{email.ConfigValue = row.Email.ToString();} catch{email.ConfigValue ="";} try{loginNameText.ConfigValue = row.LoginName.ToString();} catch{loginNameText.ConfigValue ="";} try{fullNameTextBox.ConfigValue = row.Name.ToString();} catch{fullNameTextBox.ConfigValue ="";} try{companyTextBox.ConfigValue = row.Company.ToString();} catch{companyTextBox.ConfigValue ="";} try{phoneTextBox.ConfigValue = row.Phone.ToString();} catch{phoneTextBox.ConfigValue ="";} try{addressTextBox.ConfigValue = row.Address.ToString();} catch{addressTextBox.ConfigValue ="";} try{enableUserCheckBox.Checked = !row.Disabled;} catch{enableUserCheckBox.Checked = false;} try{passwordTextBox.ConfigValue ="******";} catch{passwordTextBox.ConfigValue ="******";} } finally { this.IsDirty = false; }
[Advertisement]
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!