抛出异常或处理异常?(Throw exception or treat exception? [closed])
如果有一个接收null或意外值的方法,我该怎么办:
- 将值转换为正常值(处理异常)或
- 抛出异常,因为值无效?
What should I do if there is a method that receives null or unexpected values:
- Convert the values to a normal value (treat exception) or
- Throw an exception because the values are invalid?
原文:https://stackoverflow.com/questions/20720549
最满意答案
由于文本框可能绑定到代码隐藏中的double属性,我认为有一些聪明的转换前验证可以提供您看到的验证错误。 为了控制验证过程,我认为有几种方法可以解决它:
- 您可以使自己的验证规则
- 您可以从转换器返回validationresult(虽然IMO验证和转换似乎并不真正属于一起)
- 您可以使用IDataError接口和MVVM
以下是一些提供一些有用信息的链接:
还有一件事,当将输入转换为double时,是否有任何特殊的理由使用Invariant文化而不是作为参数传递的文化? 它可能在非英语文化(例如丹麦语)上失败,其中小数点分隔符是逗号(相当于英语文化中的千位分隔符)。
Since the textbox is probably bound to a double property in the code-behind, I think there is some clever pre-conversion validation which gives the validation error you see. To control the validation process, I think there are several ways to go about it:
- You can make you own validation rule
- You can return a validationresult from the converter (although IMO validation and conversion don't really seem to belong together)
- You can make use of the IDataError interface and MVVM
Here are a couple of links which provide some good information:
- Using a ViewModel to Provide Meaningful Validation Error Messages
- Data validation in WPF
- Using Custom Validation Rules in WPF
One more thing, when converting the input to double, is there any particular reason to use Invariant culture instead of the culture passed as parameter? It might fail on a non-english culture (e.g. Danish), where the decimal separator is a comma (which is equivalent to thousand separator in English culture).
相关问答
更多-
获得经度和纬度(Getting longitude and latitude)[2023-06-05]
您需要在委托方法中检查位置 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { manager.stopUpdatingLocation() // if you dont want continuously update currentLocation = manager.location let locat ... -
我不确定这是你想要的,但它是我目前用来获得一个很好的可读地址。 我还将这些值发送给我的应用程序委托,以便稍后在需要时进行引用。 我没有用Swift写的,只是在Objective C中。:/但我认为关键是reverseGeocodeLocation #pragma mark - location Manager update and FUNCTIONS - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError ...
-
由于你的locationManager变量myLat和myLong仅仅是局部函数,所以声明两个全局变量(a)由locationManager设置,然后(b)由IBAction函数提取。 Since your locationManager variables myLat and myLong are local only to that function, just declare two global variables that are (a) set by locationManager and (b ...
-
首先看看您是否有任何格式不正确的数据: select * from san_filia1 wHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17) and (isnumeric(lat) = 0 or isnumeric(long) = 0) 从这里,您将看到导致问题的原因,然后您可以解决它。 正确的解决方案可能是这些方面: select * from (select sf.*, (case when isnum ...
-
显示启动画面直到获得纬度和经度(phonegap)(showing splash screen until latitude and longitude are got (phonegap))[2022-05-05]
@jerome,你可以做你想要的事情,但是在那个闪屏消失之前可能是一个漫长的时间。 GPS无法正常工作。 我建议你做很多阅读GPS。 你的误解很常见。 对不起,我没有任何好的文章供你阅读。 这篇文章可能是为了技术,但试试Garmin | 什么是GPS? 在此期间,您可以获得尝试使用我的演示应用程序所需的内容。 Phonegap演示应用程序 http://codesnippets.altervista.org/examples/phonegap/demos/PUBLIC.Apps.html 你想要结合: Ph ... -
我找到了答案:我在AVD API 22上试过我的代码,它工作的很好,所以这个代码在API 23之前是好的,那么你必须使用另一个代码。 如果我找到一个工作代码,我会发布它。 编辑 代码很好,只是在api> = 23中,你必须询问用户是否使用“危险许可” I found the answer : I tried my code on an AVD API 22 and it works just fine so this code is good until API 23, then you must use ...
-
由于文本框可能绑定到代码隐藏中的double属性,我认为有一些聪明的转换前验证可以提供您看到的验证错误。 为了控制验证过程,我认为有几种方法可以解决它: 您可以使自己的验证规则 您可以从转换器返回validationresult(虽然IMO验证和转换似乎并不真正属于一起) 您可以使用IDataError接口和MVVM 以下是一些提供一些有用信息的链接: 使用ViewModel提供有意义的验证错误消息 WPF中的数据验证 在WPF中使用自定义验证规则 还有一件事,当将输入转换为double时,是否有任何特殊的 ...
-
您的代码的问题是地理编码请求是异步的,因此在实际检索地理编码结果之前执行return语句。 我可能会使用两个选项中的一个来解决这个问题。 首先,不要返回元组,而是创建自己的完成处理程序,并在找到地标后调用它: func forwardGeocoding (address: String, completion: (CLLocationCoordinate2D) -> Void) { let geoCoder = CLGeocoder() geoCoder.geocodeAddressStri ...
-
经度和纬度为空(Latitude and Longitude are null)[2022-09-15]
问题 问题出在GeoTagTask里面, Location location; private String lat = Double.toString(location.getLatitude()); private String lng = Double.toString(location.getLongitude()); 您正在访问未初始化的变量。 解 使onCreate位置成为全局变量, public class MainActivity extends FragmentActivity impl ... -
Lat和lon仅在onLocationchanged中设置为各自的值,在地理编码后触发,尝试设置之前的lat和lon或将地理编码放在onLocationChanged中。 我看不到所有代码,但我猜你正在将lat和lon初始化为0,而地理编码器没有0,0的地址 Lat and lon are only being set to their respective values in onLocationchanged which fires after your geocoding, try either se ...