fix #19580; add warning for bare except: clause (#21099)

* 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:
ringabout 2022-12-15 13:45:36 +08:00 • committed by GitHub
commit 91ce8c385d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 50 additions and 26 deletions

View file

@ -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
```

View file

@ -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()