finally 权重大于return

本文深入探讨了Java中的try-catch-finally结构,解析了在使用return语句强制函数返回的情况下,finally块为何仍然执行。通过具体代码示例,揭示了Java异常处理机制的内在逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String s = "1";
try{
s = "2";
System.out.println(s);
return;
}catch(Exception e){
s = "3"; 
System.out.println(s);
}finally{
s = "4";
System.out.println(s);

s = "5";
System.out.println(s);
输出的结果是2,4
也就是说在try结构中,虽然使用了return语句强制函数返回,不再往下执行,但实现上finally中的还是执行了。但除了finally外的其它语句不再被执行。
import pandas as pd from wordcloud import WordCloud import matplotlib.pyplot as plt import os from collections import Counter from matplotlib.font_manager import FontProperties # 维度映射 DIMENSIONS = { "年龄": "age", "人才标签": "tags", "学历专业": "major", "学历": "education", "期望岗位": "expect_pos", "过往任职岗位": "position" } # 指定字体路径 FONT_PATH = '/System/Library/Fonts/PingFang.ttc' font_prop = FontProperties(fname=FONT_PATH) # 设置matplotlib全局字体 plt.rcParams['font.family'] = font_prop.get_name() plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 def generate_wordcloud(text, title, filename): """生成并保存词云""" try: if not text or len(text.strip()) < 3: # 至少需要3个字符 print(f"跳过:{title}无足够文本内容") return wordcloud = WordCloud( font_path=FONT_PATH, width=800, height=600, background_color="white", max_words=200, collocations=False, font_step=1 # 提高字体清晰度 ).generate(text) plt.figure(figsize=(10, 8)) plt.imshow(wordcloud, interpolation="bilinear") plt.title(title, fontproperties=font_prop, fontsize=15) plt.axis("off") # 保存高清图片 output_path = f"wordclouds-zhipin/{filename}.png" plt.savefig(output_path, bbox_inches='tight', dpi=300) plt.close() print(f"成功生成: {output_path}") except Exception as e: print(f"生成{title}词云失败: {str(e)}") def process_dimension_data(df, dimension_name): """增强的数据处理函数""" # 筛选并清理数据 dim_data = df[df["维度"] == dimension_name]["维度结果"].dropna() # 处理数值型年龄数据 if dimension_name == "年龄": dim_data = dim_data.apply(lambda x: f"{int(float(x))}岁" if str(x).isdigit() else str(x)) # 转换为有效文本 values = [str(v).strip() for v in dim_data if str(v).strip()] # 统计词频(考虑长度大于1的有效词) if values: freq = Counter(v for v in values if len(v) > 1) if freq: return " ".join([f"{word} " * count for word, count in freq.items()]) return None # 主程序 def main(): os.makedirs("wordclouds-zhipin", exist_ok=True) try: df = pd.read_excel("zhipin_result.xlsx", sheet_name="全部维度结果次数") print(f"成功读取文件,共{len(df)}条记录") # 数据质量检查 if "维度" not in df.columns or "维度结果" not in df.columns: raise ValueError("文件必须包含'维度'和'维度结果'列") print("\n各维度数据量统计:") print(df["维度"].value_counts()) # 处理每个维度 for dim_cn, dim_en in DIMENSIONS.items(): print(f"\n处理维度: {dim_cn}") text = process_dimension_data(df, dim_cn) if text: print(f"有效文本长度: {len(text)}") generate_wordcloud(text, dim_cn, f"{dim_en}_wordcloud") else: print(f"警告:{dim_cn}维度无有效数据") except Exception as e: print(f"程序出错: {str(e)}") finally: print("\n处理完成!请查看wordclouds-zhipin文件夹") if __name__ == "__main__": main() 帮我优化代码,并提高高频词的命中率,自定义形状,并给出完整代码
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值