Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"throws" and "annotation for exception" in Dalvik bytecode

Why the byte code is generated as

 .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/FileNotFoundException;
        }
 .end annotation

rather than .throws Ljava/io/FileNotFoundException

if a method declares throws FileNotFoundException in the header in java code?

like image 990
monica Avatar asked Dec 27 '12 23:12

monica


2 Answers

The short answer is that there is no specific "throws" concept in the dex format. When a java classfile is converted to the dex format, a Throws annotation is added that contains this information.

Slightly longer answer:

The concept of a checked exception only matters at compile time, not at runtime. The dalvik virtual machine doesn't know about or care what exceptions your method can throw. As far as it is concerned, everything is an unchecked exception. It's the java compiler that enforces that checked exceptions are declared in your throws clause.

As such, it doesn't make sense to add a specific "throws" concept to the dex file. Instead, that information is stored using the more generic annotation feature.

It sounds like you are using something like dex2jar to convert a dex file back to a set of class file and then using jasmin on it. It's likely that dex2jar doesn't remap the Throws annotations from the dex file back to the Exception attribute in the classfile, although I haven't specifically checked whether that is the case.

like image 137
JesusFreke Avatar answered Oct 04 '22 16:10

JesusFreke


dalvik.annotation.Throws appears on methods

A Throws annotation is attached to each method which is declared to throw one or more exception types.

At the bottom of this page.

like image 30
Snicolas Avatar answered Oct 04 '22 17:10

Snicolas