注释和异常 [英] Annotation and exception

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

问题描述

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

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?

非常感谢.

推荐答案

注释可以通过两种方式处理:

Annotations can be processed in two ways:

  • 在编译时:它们实际上导致将不同/附加的字节码写入类文件中
  • 在运行时:通常,某种框架会检查方法/类/...上注释的存在性,然后运行不同的代码,具体取决于该特定注释是否到位是否
  • 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天全站免登陆