forked from zhravan/golearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecover.go
More file actions
25 lines (20 loc) · 688 Bytes
/
Copy pathrecover.go
File metadata and controls
25 lines (20 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package recover_exercise
// DoWork simulates a function that might panic if the input is negative.
func DoWork(n int) {
if n < 0 {
// A panic occurs if n is negative
panic("input cannot be negative")
}
// Normal, non-panicking code path...
}
// Run should call DoWork and safely recover from any panic
// that occurs during its execution, returning the recovered value.
// It should return nil if no panic occurred.
func Run(n int) (recoveredValue interface{}) {
// 1. Add your 'defer' statement here.
// 2. The deferred function should call recover() and assign the result
// to 'recoveredValue' if it is not nil.
// Your code here:
DoWork(n)
return recoveredValue
}