首页 \ 问答 \ 抛出异常或处理异常?(Throw exception or treat exception? [closed])

抛出异常或处理异常?(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
更新时间:2023-02-18 10:02

最满意答案

由于文本框可能绑定到代码隐藏中的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:

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

相关问答

更多
  • 您需要在委托方法中检查位置 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 ...
  • @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 ...
  • 问题 问题出在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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。