throws_lints 1.0.0
throws_lints: ^1.0.0 copied to clipboard
Lint rules that ensure functions throwing exceptions are properly documented with @throws annotations.
example/throws_lints_example.dart
// Examples demonstrating the throws_lints rules
import 'package:throws_lints/throws_lints.dart';
// ============================================================================
// Example 1: missing_throws_annotation
// Functions that throw directly must have @throws annotation
// ============================================================================
// ❌ BAD - throws without @throws annotation
void badDirectThrow() {
throw Exception('Missing annotation!');
}
// ✅ GOOD - has @throws annotation
@throws
void goodDirectThrow() {
throw Exception('Properly annotated');
}
// ❌ BAD - rethrow without @throws annotation
void badRethrow() {
try {
riskyOperation();
} catch (e) {
rethrow;
}
}
// ✅ GOOD - rethrow with @throws annotation
@throws
void goodRethrow() {
try {
riskyOperation();
} catch (e) {
rethrow;
}
}
// ============================================================================
// Example 2: throwing_function_calls_require_throws
// Functions that call @throws functions must also have @throws annotation
// ============================================================================
@throws
void throwingHelper() {
throw Exception('Helper throws');
}
// ❌ BAD - calls throwing function without @throws annotation
void badCallsThrowingFunction() {
throwingHelper(); // This can throw!
}
// ✅ GOOD - has @throws annotation when calling throwing function
@throws
void goodCallsThrowingFunction() {
throwingHelper();
}
// ✅ GOOD - catches the exception, so no @throws needed
void catchesThrowingFunction() {
try {
throwingHelper();
} catch (e) {
print('Caught: $e');
}
}
// ============================================================================
// Example 3: unnecessary_throws_annotation
// Don't use @throws annotation if the function doesn't actually throw
// ============================================================================
// ❌ BAD - has @throws but doesn't throw
@throws
void badUnnecessaryAnnotation() {
print('This is safe');
}
// ✅ GOOD - no annotation for safe function
void goodSafeFunction() {
print('This is safe');
}
// ✅ GOOD - has @throws and actually throws
@throws
void goodActuallyThrows() {
throw Exception('This really throws');
}
// ❌ BAD - has @throws but all exceptions are caught
@throws
void badAllCaught() {
try {
throw Exception('Caught internally');
} catch (e) {
print('Handled: $e');
}
}
void riskyOperation() {
// Helper function for examples
}