README.md (2524B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # trycatch 22 23 > If a function does not throw, return the function return value; otherwise, return `y`. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var trycatch = require( '@stdlib/utils/try-catch' ); 41 ``` 42 43 #### trycatch( x, y ) 44 45 If a function `x` does not throw, returns the return value of `x`; otherwise, returns `y`. 46 47 ```javascript 48 function x1() { 49 return 1.0; 50 } 51 52 var z = trycatch( x1, -1.0 ); 53 // returns 1.0 54 55 function x2() { 56 throw new Error( 'beep' ); 57 } 58 59 z = trycatch( x2, -1.0 ); 60 // returns -1.0 61 ``` 62 63 </section> 64 65 <!-- /.usage --> 66 67 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 68 69 <section class="notes"> 70 71 </section> 72 73 <!-- /.notes --> 74 75 <!-- Package usage examples. --> 76 77 <section class="examples"> 78 79 ## Examples 80 81 <!-- eslint no-undef: "error" --> 82 83 ```javascript 84 var randu = require( '@stdlib/random/base/randu' ); 85 var trycatch = require( '@stdlib/utils/try-catch' ); 86 87 var z; 88 var i; 89 90 function x() { 91 if ( randu() < 0.9 ) { 92 throw new Error( 'BOOP' ); 93 } 94 return 'BOOP'; 95 } 96 97 for ( i = 0; i < 100; i++ ) { 98 z = trycatch( x, 'beep' ); 99 console.log( z ); 100 } 101 ``` 102 103 </section> 104 105 <!-- /.examples --> 106 107 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 108 109 <section class="references"> 110 111 </section> 112 113 <!-- /.references --> 114 115 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 116 117 <section class="links"> 118 119 </section> 120 121 <!-- /.links -->