@@ -64,10 +64,9 @@ func ClassicCopyFile(src, dst string) error {
64
64
return nil
65
65
}
66
66
67
- // OrgCopyFile copies the source file to the given destination. If any error occurs it
67
+ // TryCopyFile copies the source file to the given destination. If any error occurs it
68
68
// returns an error value describing the reason.
69
- func OrgCopyFile (src , dst string ) (err error ) {
70
- defer err2 .Handle (& err ) // automatic error message: see err2.Formatter
69
+ func TryCopyFile (src , dst string ) {
71
70
// You can out-comment above handler line(s) to see what happens.
72
71
73
72
// You'll learn that call stacks are for every function level 'catch'
@@ -79,17 +78,13 @@ func OrgCopyFile(src, dst string) (err error) {
79
78
r := try .To1 (os .Open (src ))
80
79
defer r .Close ()
81
80
82
- w , err := os .Create (dst )
83
- if err != nil {
84
- return fmt .Errorf ("mixing traditional error checking: %w" , err )
85
- }
81
+ w := try .To1 (os .Create (dst ))
86
82
defer err2 .Handle (& err , func (err error ) error {
87
83
try .Out (os .Remove (dst )).Logf ("cleaning error" )
88
84
return err
89
85
})
90
86
defer w .Close ()
91
87
try .To1 (io .Copy (w , r ))
92
- return nil
93
88
}
94
89
95
90
func CallRecur (d int ) (ret int , err error ) {
@@ -161,13 +156,13 @@ func doMain() (err error) {
161
156
// how err2 works. Especially interesting is automatic stack tracing.
162
157
//
163
158
// source file exists, but the destination is not in high probability
164
- //try.To(OrgCopyFile( "main.go", "/notfound/path/file.bak") )
159
+ //TryCopyFile( "main.go", "/notfound/path/file.bak")
165
160
166
161
// Both source and destination don't exist
167
- //try.To(OrgCopyFile( "/notfound/path/file.go", "/notfound/path/file.bak") )
162
+ //TryCopyFile( "/notfound/path/file.go", "/notfound/path/file.bak")
168
163
169
164
// to play with real args:
170
- try . To ( CopyFile ( flag .Arg (0 ), flag .Arg (1 ) ))
165
+ TryCopyFile ( flag .Arg (0 ), flag .Arg (1 ))
171
166
172
167
if len (flag .Args ()) > 0 {
173
168
// Next fn demonstrates how error and panic traces work, comment out all
@@ -177,7 +172,7 @@ func doMain() (err error) {
177
172
fmt .Println ("ret val:" , ret )
178
173
} else {
179
174
// 2nd argument is empty to assert
180
- try . To ( OrgCopyFile ( "main.go" , "" ) )
175
+ TryCopyFile ( "main.go" , "" )
181
176
}
182
177
183
178
fmt .Println ("=== you cannot see this ===" )
0 commit comments