注释和异常 [英] Annotation and exception

查看:19
本文介绍了注释和异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读错误日志文件,并检查错误行是否引用了注释.我在没有结果的文档上尝试过,但是注释(或自定义注释)会抛出异常吗?

I'm reading the error log file and I'm checking if error line refers to the annotation. I tried on the doc without results, but can the annotation (or a custom annotation) throw an exception?

非常感谢.

推荐答案

注解有两种处理方式:

  • 在编译时:它们实际上导致将不同/额外的字节码写入类文件
  • 在运行时:通常,某种框架会检查方法/类/...上注解的存在,然后运行不同的代码,具体取决于该特定注解是否到位与否
  • at compile time: they actually result in different/additional byte code being written into class files
  • at run time: typically, some sort of framework checks for the presence of an annotation on a method/class/... to then run different code, depending if that specific annotation is in place or not

换句话说:注解本身并不类似于任何真正的代码,它只是一个标记".但是当然,为了做一些有用的事情(尤其是在运行时),当存在注释时,可能会有一些代码做一些不同的事情.当然,该代码可以抛出异常.但在这种情况下,您应该会收到堆栈跟踪信息,以及希望有帮助的消息.

In other words: the annotation itself doesn't resemble to any real code, it is just a "marker". But of course, in order to do something useful (especially at runtime), there is probably some code that does something different when annotations are present. And of course, that code can throw exceptions. But in that case, you should receive a stack trace, and hopefully helpful messages.

从这个角度来看:注解本身不能抛出异常,因为注解本身并不类似于可以执行"的东西.或者从 J-Alex 的另一个答案中窃取措辞:注释可能导致异常,但它们不能成为源".

From that point of view: the annotation itself can't throw an exception, because the annotation itself doesn't resemble something that can be "executed". Or to steal wording from the other answer by J-Alex: annotations can cause exceptions, but they can't be the "source".

这篇关于注释和异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆