What are the keywords of java exception handling
What is an exception?
Some exceptions are caused by user errors, some are caused by program errors, and others are caused by physical errors.
Exception handling keywords:
try, catch, finally, throw, throws
Notes:
1. Error is not an exception, but a breakaway A matter of programmer control.
2. All exception classes are subclasses inherited from the java.lang.Exception class.
3. The exception class has two main subclasses: IOException class and RuntimeException class.
4. Java has many built-in exception classes.
(Video tutorial recommendation: java video)
Grammar:
try{ //需要监听的代码块 } catch(异常类型 异常名称/e){ //对捕获到try监听到的出错的代码块进行处理 throw 异常名称/e; //thorw表示抛出异常 throw new 异常类型(“自定义”); } finally{ //finally块里的语句不管异常是否出现,都会被执行 } 修饰符 返回值 方法名 () throws 异常类型{ //throws只是用来声明异常,是否抛出由方法调用者决定 //代码块 }
Sample code: (try, catch and finally)
public class ExceptionTest { public static void main(String[] args) { Scanner input=new Scanner(System.in); try{ //监听代码块 int a=input.nextInt(); int b=input.nextInt(); double sum=a/b; System.out.println(sum); } catch(InputMismatchException e){ System.out.println("只能输入数字"); } catch(ArithmeticException e){ System.out.println("分母不能为0"); } catch(Exception e){ //Exception是所有异常的父类 System.out.println("发生了其他异常"); } finally{ //不管是否出现异常,finally一定会被执行 System.out.println("程序结束"); } } }
Sample code: (throw keyword)
import java.util.InputMismatchException; import java.util.Scanner; public class ExceptionTest { public static void main(String[] args) { Scanner input=new Scanner(System.in); try{ //监听代码块 int a=input.nextInt(); int b=input.nextInt(); double sum=a/b; System.out.println(sum); } catch(InputMismatchException e){ //catch(异常类型 异常名称) System.out.println("只能输入数字"); throw e; //抛出catch捕捉到的异常 //throw new InputMismatchException(); 同上 } catch(ArithmeticException e){ System.out.println("分母不能为0"); throw new ArithmeticException("分母为0抛出异常"); //抛出ArithmeticException异常 } catch(Exception e){ //Exception是所有异常的父类 System.out.println("发生了其他异常"); } finally{ //不管是否出现异常,finally一定会被执行 System.out.println("程序结束"); } } }
Sample code: (throws)
public class Throws { int a=1; int b=0; public void out() throws ArithmeticException{ //声明可能要抛出的异常,可以有多个异常,逗号隔开 try{ //监听代码块 int sum=a/b; System.out.println(sum); } catch(ArithmeticException e){ System.out.println("分母不能为0"); } finally{ //不管是否出现异常,finally一定会被执行 System.out.println("程序结束"); } } public static void main(String[] args){ Throws t=new Throws(); t.out(); //调用方法 throw new ArithmeticException("分母为0抛出异常"); //由调用的方法决定是否要抛出异常 /* * 第二种抛出方式 */ // ArithmeticException a=new ArithmeticException("分母为0抛出异常"); // throw a; } }
Recommended tutorial: java entry program
The above is the detailed content of What are the keywords of java exception handling. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
