Skip to content

Instantly share code, notes, and snippets.

View madmag77's full-sized avatar

Goncharov Artem madmag77

View GitHub Profile
@madmag77
madmag77 / dart_rust_exception_example.rs
Created March 5, 2021 11:36
[Dart article] Rust exception example
use std::fs::File;
fn main() {
let f = File::open("main.jpg"); // main.jpg doesn't exist
match f {
Ok(f)=> {
println!("file found {:?}",f);
},
Err(e)=> {
println!("file not found \n{:?}",e); //handled error
}
@madmag77
madmag77 / dart_go_exception_sample.go
Created March 5, 2021 11:30
Dart article] Example of exception in Go
func Sqrt(f float64) (float64, error) {
if f < 0 {
return 0, errors.New("math: square root of negative number")
}
// implementation
}
f, err := Sqrt(-1)
if err != nil {
fmt.Println(err)
@madmag77
madmag77 / dart_exceptions_example.dart
Last active March 6, 2021 08:37
[Dart article] Exceptions in dart language
try {
final remaingAmount = await getRemainingAmount(account: account);
displayAccountAmount(remaingAmount);
} on SocketException catch (_) {
displayNoConnectionError();
} catch (e, s) {
logException(exception: e, stackTrace: s);
rethrow;
}
@madmag77
madmag77 / dart_func.dart
Last active March 6, 2021 08:37
[Dart article] Dart function
Future<Money> getRemainingAmount({Account account}) async {
final response = await backend.getAcccountInfo(account);
return response.remainingAmount;
}
@madmag77
madmag77 / dart_unhandled_exceptions_flutter.dart
Created March 5, 2021 10:34
[Dart article] Unhandled Exceptions in Flutter
void main() {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (FlutterErrorDetails errorDetails) {
print("onError Exception: $errorDetails was caught by Flutter framework - redirect to Sentry or Firebase.");
};
runZonedGuarded(() {
createProviders();
runApp(MyApp());
@madmag77
madmag77 / dart_streams2.dart
Last active March 6, 2021 09:44
[Dart article] Exceptions in streams 2
import 'dart:async';
void main(List<String> arguments) async {
Stream<int> stream;
runZonedGuarded(() {
stream = getStream();
}, (e, s) {
print('PRODUCER ZONE Exception handled: $e, $s');
});
@madmag77
madmag77 / dart_streams1.dart
Last active March 6, 2021 09:43
[Dart article] Exceptions in streams 1
import 'dart:async';
void main(List<String> arguments) async {
startListener(getStream());
}
Stream<int> getStream() {
final intsController = StreamController<int>.broadcast();
int i = 42;
Timer.periodic(Duration(seconds: 1), (timer) {
@madmag77
madmag77 / dart_zones2.dart
Created March 5, 2021 10:10
[Dart article] Guarded zones 2
print('Current zone start in: ${Zone.current.toString()}');
runZonedGuarded(() {
print(
'Current zone inside runZoned: ${Zone.current.toString()} with name ${Zone.current['ZoneName']}');
Timer.run(() {
print('Timer runs.');
throw Exception('[Timer] Bad thing happens!');
});
runZonedGuarded(
() {
@madmag77
madmag77 / dart_zones1.dart
Created March 5, 2021 09:58
[Dart article] Guarded zones 1
runZonedGuarded((){
Timer.run(() {
print('Timer runs.');
throw Exception('[Timer] Bad thing happens!');
});
print('Everything is fine!');
}, (e,s) {
print('Exception handled $e, \n$s');
});
@madmag77
madmag77 / dart_async_axceptions8.dart
Created March 5, 2021 09:23
[Dart article] Async code exceptions 8
try {
Timer.run(() {
print('Timer runs.');
throw Exception('[Timer] Bad thing happens!');
});
print('Everything is fine!');
} on IntegerDivisionByZeroException {
print('Cant divide to zero');
} finally {
print('Clean-up done');