当前位置:网站首页>I talked about exception handling for more than half an hour during the interview yesterday
I talked about exception handling for more than half an hour during the interview yesterday
2022-06-29 13:56:00 【51CTO】
The strong wind blew away the haze of the past , The sun shines through the window , Through the window, you can see the mountains in the distance and the blue sky , It's much better than my rental house where I'll never see the sun . The sound of footsteps coming in interrupted my thoughts , A little sister sat in front of her , The smell of sweet perfume immediately went into my nostrils. .
Said the little sister with a smile :” Hello! , I'm the interviewer today , So here we go ?“

I put away my straight eyes , say :“ well .”
Miss said :“ stay Java There are two major elements in exception handling : Throw and catch exceptions . So what are the two types of exceptions thrown ?”
I answered immediately :
Throwing exceptions can be divided into explicit and implicit . Explicit exception throwing is used in code throw Keyword to manually throw an exception instance . Implicit throwing exception is Java The virtual machine is executing , Encountered an exception that cannot continue execution , Automatically throw exception instances , For example, we often encounter null pointer exceptions (NullPointerException).
Miss said :“ very good , So what keywords are often used in catching exceptions ?”
I answered immediately :
We usually use try、catch、finally Other key words .try It is used to mark the code that needs exception monitoring ;catch Used to capture try An exception of a specified type that is triggered in the monitored code , You can also define how to handle this exception type ;finally Used to declare a piece of code that must run no matter what exception happens , It avoids skipping some critical cleanup code , such as : Close the opened IO resources .
Miss said :“ very good , If three keywords are used together , What is the order of code execution ?”
I answered immediately :
Under normal execution , Execute first try Execute the code in finally The code in .
If try The code in triggers an exception , And the exception is not caught ,finally The code will be executed directly , And throw the exception again after execution ; If the exception is catch Capture , Execute first catch Execute the code in finally The code in .
If catch The code in also triggers an exception , that finally The code will also be executed , And throw out catch The exception triggered by the code . If finally The code in also triggers an exception , Then it will interrupt the current finally Execution of code , And throw an exception .
Miss said :“ very good , stay Java In the virtual machine , How to implement exception handling ?”
This question is a little difficult , I thought a little and replied :
Mainly through Anomaly table . In the bytecode generated by compilation , Each method comes with an exception table . There may be multiple records in the exception table , Every record includes from The pointer 、to The pointer 、target Pointer and the type of exception caught . The values of these Pointers are bytecode indexes (bytecode index), Used to locate bytecode .
among ,from Pointers and to The pointer indicates the scope of exception handling monitoring , such as : try Coverage .target The pointer points to the starting position of the exception handling code , such as :catch The starting position of the code .
If an exception is triggered ,Java The virtual opportunity traverses all records in the exception table from top to bottom . When the index value of the bytecode triggering the exception is within the monitoring range recorded in an exception table ,Java The virtual opportunity determines whether the thrown exception matches the exception that the record wants to catch . If the match , The control flow will be moved to this record target The bytecode to which the pointer points .
Miss said :“ A bit abstract , Can you give me an example ?”
“ Certainly. .” I said , While writing on the paper :
Of this code main In the method , I defined a paragraph try and catch Code . In the compiled bytecode , The exception table of this method has a record :
Its from Pointers and to The pointers are 3 and 6, Represents its monitoring range from index to 3 The bytecode starts , To index as 6 End of bytecode ( barring 6). It should be recorded target Pointer is 9, Represents the exception handling from the index to 9 The bytecode starts . The last column of the record , Represents that the exception type caught by this exception handling is Exception.
When the index value of the bytecode triggering the exception is 3 and 6 Between time ,Java When the virtual opportunity judges whether the thrown exception is Exception. If it is , The control flow will be moved to the index 9 The bytecode of begins to execute .
The little sister said with satisfaction :“ very good , I'll call HR Talk to you , With luck , You can send it today offer.”
reference :
《Java Programming idea 》
《Java The core technology 》
《 In depth understanding of Java virtual machine :JVM Advanced features and Practice 》
边栏推荐
- 打造一个 API 快速开发平台,牛逼!
- WinDbg debugging tool introduction
- 使用 Gerrit + Zadig 实现主干开发主干发布(含字节飞书实践)
- 技术分享| 融合调度中的广播功能设计
- Xiaobai learns MySQL - incremental statistical SQL requirements - windowing function scheme
- Huawei machine learning service speech recognition function enables applications to paint "sound" and color
- grep 精确匹配
- Solution to inconsistency between RTC time and world time on the server
- Exploring the way of automated testing - Preparation
- Memorized Function
猜你喜欢

Hundreds of CVPR people were recruited as new champions. Emoji became a "court witness". M2 MBP was exposed that the hard disk speed was reduced. Today, more big news is here

深度学习的坎坷六十年
![[document translation] camouflaged object detection](/img/30/73a927c05173a95cc5a5d51e182e3b.png)
[document translation] camouflaged object detection

别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!

Qitai observation: professional elites must step on the huge pit of entrepreneurship - learning effect pit

Xiaobai learns MySQL - incremental statistical SQL requirements - windowing function scheme

WinDbg common commands

如何让 Dapper 支持 DateOnly 类型

灵感收集·创意写作软件评测:Flomo、Obsidian Memo、Napkin、FlowUs
![[untitled] error in installation dependency: refusing to install package with name](/img/53/8c871037b7586343fd509dcecb0d96.png)
[untitled] error in installation dependency: refusing to install package with name "* * *" under a package
随机推荐
Cloud native (31) | kubernetes chapter kubernetes platform basic pre installed resources
Force buckle: merging two ordered linked lists
Technology sharing | broadcast function design in integrated dispatching
Create an API rapid development platform, awesome!
Normalization layer of pytorch learning (batchnorm, layernorm, instancenorm, groupnorm) [easy to understand]
Lecun published a research plan for the next decade: AI autonomous intelligence with a 62 page paper
Package folders as ISO
[cloud resident co creation] break through the performance bottleneck of image recognition through rust language computing acceleration technology
windows平台下的mysql启动等基本操作
Write a shell script to find the "reverse order" of a number“
GBase8s数据库遇到951错误是什么原因?
成功解决NotImplementedError: numpy() is only available when eager execution is enabled.
机器学习 Out-of-Fold 折外预测详解 | 使用折外预测 OOF 评估模型的泛化性能和构建集成模型
Can I open an account online? Is it safe
matplotlib的imshow函数显示灰度图像要设置vmin和vmax2个参数
Hash hash game system development explanation technology -- hash game system development solution analysis
[graduation season] it's worth it all the way over the past four years -- advice from the senior students
Windwos10 installing sshd service
DeeCamp2022正式开营!李开复、张亚勤亲授大师课 | 创新事
维修记录导出的excel表格太大怎么办?