What?
No.
You could use method overloading, something like
public void handle(FunnyException e) {
...
}
public void handle(NastyException e) {
...
}
, and when calling handle
, the most appropriate method will be called.
:
However, I would say that you basically do three things with Exceptions:
- Propagate them using
throws
- Propagate them wrapping them in another kind of Exception
- Catch them and do something about them
1 and 2 are not worth it; calling a generic exception handler would be more verbose than just writing the code required.
3 tends to be longer code, however, it is very unusual that you can catch and treat exceptions in the same way in different parts of your code; if you do, this tends to be a code smell; perhaps you need a broader try statement or some disjoint code needs to be brought together.
So I don't think there's a reason to do what you describe.
Python lambda
expressions are real, formal untyped λ-calculus lambda expressions.
They fit the formal definition; they can only represent one python expression, based on variables (free or otherwise) and references to other functions (abstract symbols). Python uses parenthesis in expressions too.
You use them wherever a lambda
is more suitable and convenient than a full function definition. The python def functionname(argumentlist):
syntax forms a statement; in Python you cannot put statements inside of expressions, only the other way around. A lambda
on the other hand, is an expression, so you can use a lambda
to insert a callback function inline:
map(lambda x, y: x[y+5], [(mapping1, integerkey1), (mapping2, integerkey2)])
The above example consists only of an expression. The python map()
function takes, as its first argument, a callable, which is applied to each and every element in the list given by the second argument. In the above example, using a lambda
expression to define that callable is much easier than using a function statement:
def mapcallback(x, y):
return x[y + 5]
map(mapcallback, [(mapping1, integerkey1), (mapping2, integerkey2)])
For the full function syntax I need to assign a name, put the function definition on separate lines, and use the return
statement to return the result of the expression.
Best Answer
There are a number of criteria that should make you consider not using a lambda:
priceIsOver100
.x -> x.price > 100
is just as clear as that name. I mean names likeisEligibleVoter
that replace a long list of conditions.Don't go overboard. Remember, software is easily changed. When in doubt, write it both ways and see which is easier to read.