Path: blob/main/python/pylang/src/output/exceptions.py
1398 views
# vim:fileencoding=utf-81# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>2from __python__ import hash_literals34from output.statements import print_bracketed567def print_try(self, output):8else_var_name = None910def update_output_var(output):11output.indent(), output.assign(else_var_name), output.print(12'true'), output.end_statement()1314if self.belse:15else_var_name = output.new_try_else_counter()16output.assign('var ' + else_var_name), output.print(17'false'), output.end_statement(), output.indent()18output.print("try")19output.space()20print_bracketed(self, output, False, None, None,21update_output_var if else_var_name else None)22if self.bcatch:23output.space()24print_catch(self.bcatch, output)2526if self.bfinally:27output.space()28print_finally(self.bfinally, output, self.belse, else_var_name)29elif self.belse:30output.newline()31print_else(self.belse, else_var_name, output)323334def print_catch(self, output):35output.print("catch")36output.space()37output.with_parens(lambda: output.print("ρσ_Exception"))38output.space()3940def f_exception():41output.indent()42output.spaced('ρσ_last_exception', '=',43'ρσ_Exception'), output.end_statement()44output.indent()45no_default = True46for i, exception in enumerate(self.body):47if i:48output.print("else ")4950if exception.errors.length:51output.print("if")52output.space()5354def f_errors():55for i, err in enumerate(exception.errors):56if i:57output.newline()58output.indent()59output.print("||")60output.space()6162output.print("ρσ_Exception")63output.space()64output.print("instanceof")65output.space()66if err.name is 'Exception':67output.print('Error')68else:69err.print(output)7071output.with_parens(f_errors)72output.space()73else:74no_default = False75print_bracketed(exception, output, True)76output.space()77if no_default:78output.print("else")79output.space()8081def f_throw():82output.indent()83output.print("throw")84output.space()85output.print("ρσ_Exception")86output.semicolon()87output.newline()8889output.with_block(f_throw)90output.newline()9192output.with_block(f_exception)939495def print_finally(self, output, belse, else_var_name):96output.print("finally")97output.space()98if else_var_name:99100def f_try():101output.indent(), output.print("try")102output.space()103output.with_block(lambda: print_else(belse, else_var_name, output))104print_finally(self, output)105106output.with_block(f_try)107else:108print_bracketed(self, output)109110111def print_else(self, else_var_name, output):112output.indent(), output.spaced('if', '(' + else_var_name + ')')113output.space()114print_bracketed(self, output)115116117