Skip to content

Commit 64b1856

Browse files
committed
try/catch
1 parent 375e329 commit 64b1856

File tree

7 files changed

+226
-3
lines changed

7 files changed

+226
-3
lines changed

README.md

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ This guide full of examples is intended for people learning Go that are coming f
9898
- [writing](#streams)
9999
<!--
100100
- [transform](#streams)
101-
- [try/catch](#try-catch)
102101
- [concurrency](#concurrency)
103102
- [threads](#concurrency)
104103
- [forking](#concurrency)
@@ -107,6 +106,8 @@ This guide full of examples is intended for people learning Go that are coming f
107106
- [first-class functions](#first-class-functions)
108107
-->
109108
- [errors](#errors)
109+
- [try/catch](#try-catch)
110+
- [exceptions](#exceptions)
110111
- [regex](#regex)
111112
- [exec (sync)](#exec-sync)
112113
- [exec (async)](#exec-async)
@@ -134,8 +135,6 @@ This guide full of examples is intended for people learning Go that are coming f
134135
- [testing](#testing)
135136
- [benchmarking](#benchmarking)
136137
<!--
137-
- [exceptions](#exceptions)
138-
(catch panic)
139138
- [tty](#tty)
140139
- [db](#db)
141140
- [postgres](#postgres)
@@ -859,6 +858,13 @@ console.log(sub)
859858
const sub2 = array.subarray(2,4)
860859
console.log(sub2)
861860

861+
console.log(array)
862+
const value = 9
863+
const start = 5
864+
const end = 10
865+
array.fill(value, start, end)
866+
console.log(array)
867+
862868
console.log(array.byteLength)
863869
```
864870

@@ -869,6 +875,8 @@ Uint8Array [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
869875
Uint8Array [ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0 ]
870876
Uint8Array [ 2, 3, 0, 0, 0, 0, 0, 0 ]
871877
Uint8Array [ 2, 3 ]
878+
Uint8Array [ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0 ]
879+
Uint8Array [ 0, 1, 2, 3, 0, 9, 9, 9, 9, 9 ]
872880
10
873881
```
874882

@@ -894,6 +902,15 @@ func main() {
894902
sub2 := array[2:4]
895903
fmt.Println(sub2)
896904

905+
fmt.Println(array)
906+
value := uint8(9)
907+
start := 5
908+
end := 10
909+
for i := start; i < end; i++ {
910+
array[i] = value
911+
}
912+
fmt.Println(array)
913+
897914
fmt.Println(len(array))
898915
}
899916
```
@@ -905,6 +922,8 @@ Output
905922
[0 1 2 3 0 0 0 0 0 0]
906923
[2 3 0 0 0 0 0 0]
907924
[2 3]
925+
[0 1 2 3 0 0 0 0 0 0]
926+
[0 1 2 3 0 9 9 9 9 9]
908927
10
909928
```
910929

@@ -2615,6 +2634,125 @@ some error
26152634
my custom error
26162635
```
26172636
2637+
### try/catch
2638+
---
2639+
2640+
#### Node.js
2641+
2642+
```node
2643+
function foo(fail) {
2644+
if (fail) {
2645+
throw Error('my error')
2646+
}
2647+
}
2648+
2649+
function main() {
2650+
try {
2651+
foo(true)
2652+
} catch(err) {
2653+
console.log(`caught error: ${err.message}`)
2654+
}
2655+
}
2656+
2657+
main()
2658+
```
2659+
2660+
Output
2661+
2662+
```bash
2663+
caught error: my error
2664+
```
2665+
2666+
#### Go
2667+
2668+
```go
2669+
package main
2670+
2671+
import (
2672+
"errors"
2673+
"fmt"
2674+
)
2675+
2676+
func foo(fail bool) error {
2677+
if fail {
2678+
return errors.New("my error")
2679+
}
2680+
2681+
return nil
2682+
}
2683+
2684+
func main() {
2685+
err := foo(true)
2686+
if err != nil {
2687+
fmt.Printf("caught error: %s\n", err.Error())
2688+
}
2689+
}
2690+
```
2691+
2692+
Output
2693+
2694+
```bash
2695+
caught error: my error
2696+
```
2697+
2698+
### exceptions
2699+
---
2700+
2701+
#### Node.js
2702+
2703+
```node
2704+
function foo() {
2705+
throw Error('my exception')
2706+
}
2707+
2708+
function main() {
2709+
foo()
2710+
}
2711+
2712+
process.on('uncaughtException', err => {
2713+
console.log(`caught exception: ${err.message}`)
2714+
process.exit(1)
2715+
})
2716+
2717+
main()
2718+
```
2719+
2720+
Output
2721+
2722+
```bash
2723+
caught exception: my exception
2724+
```
2725+
2726+
#### Go
2727+
2728+
```go
2729+
package main
2730+
2731+
import (
2732+
"fmt"
2733+
)
2734+
2735+
func foo() {
2736+
panic("my exception")
2737+
}
2738+
2739+
func main() {
2740+
defer func() {
2741+
if r := recover(); r != nil {
2742+
fmt.Printf("caught exception: %s", r)
2743+
}
2744+
}()
2745+
2746+
foo()
2747+
}
2748+
```
2749+
2750+
Output
2751+
2752+
```bash
2753+
caught exception: my exception
2754+
```
2755+
26182756
### regex
26192757
---
26202758

examples/exceptions.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func foo() {
8+
panic("my exception")
9+
}
10+
11+
func main() {
12+
defer func() {
13+
if r := recover(); r != nil {
14+
fmt.Printf("caught exception: %s", r)
15+
}
16+
}()
17+
18+
foo()
19+
}

examples/exceptions.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function foo() {
2+
throw Error('my exception')
3+
}
4+
5+
function main() {
6+
foo()
7+
}
8+
9+
process.on('uncaughtException', err => {
10+
console.log(`caught exception: ${err.message}`)
11+
process.exit(1)
12+
})
13+
14+
main()

examples/try_catch.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
func foo(fail bool) error {
9+
if fail {
10+
return errors.New("my error")
11+
}
12+
13+
return nil
14+
}
15+
16+
func main() {
17+
err := foo(true)
18+
if err != nil {
19+
fmt.Printf("caught error: %s\n", err.Error())
20+
}
21+
}

examples/try_catch.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function foo(fail) {
2+
if (fail) {
3+
throw Error('my error')
4+
}
5+
}
6+
7+
function main() {
8+
try {
9+
foo(true)
10+
} catch(err) {
11+
console.log(`caught error: ${err.message}`)
12+
}
13+
}
14+
15+
main()

examples/uint8_array.go renamed to examples/uint8_arrays.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,14 @@ func main() {
1717
sub2 := array[2:4]
1818
fmt.Println(sub2)
1919

20+
fmt.Println(array)
21+
value := uint8(9)
22+
start := 5
23+
end := 10
24+
for i := start; i < end; i++ {
25+
array[i] = value
26+
}
27+
fmt.Println(array)
28+
2029
fmt.Println(len(array))
2130
}

examples/uint8_array.js renamed to examples/uint8_arrays.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ console.log(sub)
1212
const sub2 = array.subarray(2,4)
1313
console.log(sub2)
1414

15+
console.log(array)
16+
const value = 9
17+
const start = 5
18+
const end = 10
19+
array.fill(value, start, end)
20+
console.log(array)
21+
1522
console.log(array.byteLength)

0 commit comments

Comments
 (0)