Function encode_exception_handlers
pub fn encode_exception_handlers(
handlers: &[ExceptionHandler],
) -> Result<Vec<u8>>Expand description
Encodes exception handlers according to ECMA-335 II.25.4.6.
Exception handler sections are encoded after the method body with the following format:
- Section header (4 bytes for small format, 12 bytes for fat format)
- Exception handler entries (12 bytes each for small, 24 bytes each for fat)
Format selection:
- Small format: if all offsets and lengths fit in 16 bits
- Fat format: if any offset or length requires 32 bits
§Limitations
Warning: This function cannot fully encode EXCEPTION (catch) handlers. The type token
for catch clauses is written as 0 because the original metadata token is not preserved
in ExceptionHandler. This means:
- Round-trip operations (read method → modify → write back) will produce invalid exception handlers for catch blocks
- FILTER, FINALLY, and FAULT handlers encode correctly as they don’t require type tokens
- Use this function only for newly created handlers where type tokens will be resolved separately, or for handlers that don’t catch specific exception types
To properly support round-trip encoding of catch handlers, the ExceptionHandler struct
would need to preserve the original ClassToken value from parsing.
§Arguments
handlers- The exception handlers to encode
§Returns
The encoded exception handler section bytes, or an empty vector if no handlers.
§Errors
Returns an error if encoding fails or values exceed expected ranges.
§Examples
use dotscope::metadata::method::{ExceptionHandler, ExceptionHandlerFlags, encode_exception_handlers};
let handlers = vec![
ExceptionHandler {
flags: ExceptionHandlerFlags::FINALLY,
try_offset: 0,
try_length: 10,
handler_offset: 10,
handler_length: 5,
handler: None,
filter_offset: 0,
}
];
let encoded = encode_exception_handlers(&handlers)?;