首页 \ 问答 \ 使用Unsafe.getUnsafe()抛出异常throwException(Throwing an Exception using Unsafe.getUnsafe().throwException)

使用Unsafe.getUnsafe()抛出异常throwException(Throwing an Exception using Unsafe.getUnsafe().throwException)

我在java.lang.Class#newInstance0中遇到了一些有趣的代码:

// Run constructor
try {
    return tmpConstructor.newInstance((Object[])null);
} catch (InvocationTargetException e) {
    Unsafe.getUnsafe().throwException(e.getTargetException());
    // Not reached
    return null;
}

查看Unsafe.getUnsafe().throwException语句。 它看起来像一个检查的异常是从一个没有声明它被抛出的方法抛出的!

他们为什么这么做?
如果Sun开发人员可以使用这种技术,那么我们也可以这么做吗?


I came across some interesting code in java.lang.Class#newInstance0:

// Run constructor
try {
    return tmpConstructor.newInstance((Object[])null);
} catch (InvocationTargetException e) {
    Unsafe.getUnsafe().throwException(e.getTargetException());
    // Not reached
    return null;
}

Check out the Unsafe.getUnsafe().throwException statement. It looks like a checked exception is being thrown from a method which doesn't declare it to be thrown!

Why did they do this?
If the Sun developers can use this technique, is it ok for us to do so as well?


原文:https://stackoverflow.com/questions/4375828
更新时间:2024-03-08 18:03

最满意答案

连接你的VieWModel给你在测试中嘲笑它们的好处,连接你的Views看起来确实有点矫枉过正。 你不会交换你的观点和UI测试可以在你的ViewModel的模拟上完成,所以你不会真的需要接口他们我认为。


Interfacing your VieWModels gives you the benefit of mocking them in a test, interfacing your Views looks like some overkill indeed. You won't interchange your views and UI testing can be done on mocks of your ViewModel so you won't really need to interface them I think.

相关问答

更多
  • 连接你的VieWModel给你在测试中嘲笑它们的好处,连接你的Views看起来确实有点矫枉过正。 你不会交换你的观点和UI测试可以在你的ViewModel的模拟上完成,所以你不会真的需要接口他们我认为。 Interfacing your VieWModels gives you the benefit of mocking them in a test, interfacing your Views looks like some overkill indeed. You won't interchange ...
  • 我认为这更像是一个Python问题。 你当然可以看看Django抽象基类,但我建议你做更多的pythonic事情,不要担心尝试强制执行这些类接口。 要么你实现了这个方法,要么你没有实现,让调用代码处理它。 I think this is more of a Python question. You can certainly look at Django abstract base classes but I recommend you do the more pythonic thing and not ...
  • 这不是一件容易的事。 据我所知,XJC内部没有真正的“指南”。 我不得不边做边学。 您可能希望查看一些现有的XJC插件,以了解它们如何完成任务。 我也不太清楚你为什么要“删除现有的字段并将它们与其他类型一起添加”。 对我来说,这似乎与你的任务不一致。 我将在三个任务中分解此任务: 在指定的包中生成不可变的接口。 自动实现指定包的接口。 生成不可变类。 在构建中,我将定义3个执行: 生成不可变接口(使用1)。 生成可变类(正常XJC + 2)。 生成不可变类(3 + 2)。 immutable-xjc可能已经 ...
  • 它应该是 @model [Full Path of Your Model] 所以在你的情况下它可能是: @model [SolutionName].[ModelDirectoryIfAny].[Model] 编辑 @{ ViewBag.Title = "Home Page"; } --------
  • 通常情况下,领域模型是你应该与其他事物保持隔离的部分。 域模型可以使用接口,以便与外部系统隔离等。 但是,在最常见的情况下,域模型就是您想要防止外部系统,UI逻辑等恶化的影响 - 而不是相反。 因此,将领域模型上的接口没有什么理由。 Usually, the Domain Model is the part that you should keep isolated from everything else. The Domain Model may use interfaces so that it's ...
  • 使用接口与性能无关(除了开发团队的性能,即开发速度)。 更重要的是保持依赖性得到控制,并将程序的无关部分分开。 如果您直接依赖于代码中的具体类C,则该代码更难以进行单元测试等等。 如果你依赖于一个接口,那么在单元测试中创建一个模拟实现是很简单的。 当然,你可能不需要将你的类的所有方法都放到父接口中。 事实上,你可能不需要一个单一的父接口 。 分析这个类的用法(尤其是像你这样的大类)的机会是,你发现两个或更多不同的方法组,被不同的客户端使用(例如,一组客户端只查询对象状态,而另一组客户端则更新它)。 这使得创 ...
  • MVVM是一种将UI与代码分离的模式。 它不是仅用这三种类(视图,视图模型,模型)构建整个应用程序。 您仍然可以拥有工厂,引导程序,存储库等。 根据框架,第一个视图的创建方式不同。 例如在Caliburn.Micro第一个视图是在类Bootstrapper使用DisplayViewFor 通常有一个内置类负责查找具有特定名称(Views)的文件夹,该文件夹提取Views并将其与ViewModels匹配。 视图和模型按名称连接。 您必须遵循某种命名约定: Class1View连接 ...
  • 您正准备在命名空间准备好之前使用它。 两种选择。 首先,首先使用'app.js',然后取出初始化代码并将其放入最后加载的'bootstrap.js'中。 第二个选项,以及我通常会使用的选项,如果它们尚未存在,则在文件中定义您需要的命名空间。 例如 var app = app || {}; app.collection = app.collection || {}; 基本上,代码是逐个加载的。 当你说namespace.subspace时,代码期望命名空间已被定义为某种东西 - 通常是我见过的大多数情况下的 ...
  • 在您的位置类中,您需要使用ICoordinates类型的ICoordinates ,而不是Coordinates : class Location : ILocation { public ICoordinates Coordinate1 { get; set; } public ICoordinates Coordinate2 { get; set; } public Location(ICoordinates c1, ICoordinates c2) { ...
  • Aurelia鼓励使用MVVM(Model-View-ViewModel)设计模式。 products.html是您的视图 products.ts是您的ViewModel product.d.ts是您的模型,可以包含/使用多个ViewModel 我更喜欢call product-model.ts,但这个名字和你在一起。 通常需要创建一个服务类(例如:product-service.js)来向服务器发出请求,有时还需要一些业务逻辑,在这种情况下,您可以在此服务类中包含模型。 (在TypeScript中,它常见 ...

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。