* fix #19580; add warning for bare except: clause * fixes some easy ones * Update doc/manual.md * fixes docs * Update changelog.md * addition * Apply suggestions from code review Co-authored-by: Jacek Sieka <arnetheduck@gmail.com> * Update doc/tut2.md Co-authored-by: Jacek Sieka <arnetheduck@gmail.com>
This commit is contained in:
parent
9a50033d5b
commit
91ce8c385d
14 changed files with 50 additions and 26 deletions
|
|
@ -4773,8 +4773,8 @@ Example:
|
|||
echo "overflow!"
|
||||
except ValueError, IOError:
|
||||
echo "catch multiple exceptions!"
|
||||
except:
|
||||
echo "Unknown exception!"
|
||||
except CatchableError:
|
||||
echo "Catchable exception!"
|
||||
finally:
|
||||
close(f)
|
||||
```
|
||||
|
|
@ -4786,9 +4786,6 @@ listed in an `except` clause, the corresponding statements are executed.
|
|||
The statements following the `except` clauses are called
|
||||
`exception handlers`:idx:.
|
||||
|
||||
The empty `except`:idx: clause is executed if there is an exception that is
|
||||
not listed otherwise. It is similar to an `else` clause in `if` statements.
|
||||
|
||||
If there is a `finally`:idx: clause, it is always executed after the
|
||||
exception handlers.
|
||||
|
||||
|
|
@ -4806,11 +4803,11 @@ Try can also be used as an expression; the type of the `try` branch then
|
|||
needs to fit the types of `except` branches, but the type of the `finally`
|
||||
branch always has to be `void`:
|
||||
|
||||
```nim
|
||||
```nim test
|
||||
from std/strutils import parseInt
|
||||
|
||||
let x = try: parseInt("133a")
|
||||
except: -1
|
||||
except ValueError: -1
|
||||
finally: echo "hi"
|
||||
```
|
||||
|
||||
|
|
@ -4818,8 +4815,9 @@ branch always has to be `void`:
|
|||
To prevent confusing code there is a parsing limitation; if the `try`
|
||||
follows a `(` it has to be written as a one liner:
|
||||
|
||||
```nim
|
||||
let x = (try: parseInt("133a") except: -1)
|
||||
```nim test
|
||||
from std/strutils import parseInt
|
||||
let x = (try: parseInt("133a") except ValueError: -1)
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -4867,7 +4865,7 @@ error message from `e`, and for such situations, it is enough to use
|
|||
```nim
|
||||
try:
|
||||
# ...
|
||||
except:
|
||||
except CatchableError:
|
||||
echo getCurrentExceptionMsg()
|
||||
```
|
||||
|
||||
|
|
@ -5055,7 +5053,7 @@ An empty `raises` list (`raises: []`) means that no exception may be raised:
|
|||
try:
|
||||
unsafeCall()
|
||||
result = true
|
||||
except:
|
||||
except CatchableError:
|
||||
result = false
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -393,7 +393,7 @@ The `try` statement handles exceptions:
|
|||
echo "could not convert string to integer"
|
||||
except IOError:
|
||||
echo "IO error!"
|
||||
except:
|
||||
except CatchableError:
|
||||
echo "Unknown exception!"
|
||||
# reraise the unknown exception:
|
||||
raise
|
||||
|
|
@ -425,7 +425,7 @@ module. Example:
|
|||
```nim
|
||||
try:
|
||||
doSomethingHere()
|
||||
except:
|
||||
except CatchableError:
|
||||
let
|
||||
e = getCurrentException()
|
||||
msg = getCurrentExceptionMsg()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue