Detailed explanation of JAVA exception class structure (with examples)
1. The general structure of the exception class
2.Throwable is the super class of all exception classes.
3. It should be used when an error (exception) may occur in the program, and use the key to catch the exception for processing.
4. The catch exception structure is as follows:
public void test() { int num1 = 10; int num2 = 0; try { System.out.println(num1 / num2); // try可能出现错误的语句块 } catch (Exception e) { // 异常类型 并实例化一个异常类型e,用来对出现的异常进行说明 e.printStackTrace(); // 如果出现错误执行catch里面内容,否则跳过catch语句块 } finally { System.out.println("永远都会被执行,(system.exit)特殊情况除外,");// 无论是否出现异常都会执行finally语句块 } }
5. Some functions directly throw exceptions when they are declared and defined. When name is called, you need to catch the exception or continue to throw exceptions.
public static void main(String[] args) { try { test(); //调用这个方法就必须捕获异常或者继续抛出异常 } catch (Exception e) { e.printStackTrace(); } } //方法定义的时候抛出了异常 public static void test() throws Exception { int num1 = 10; int num2 = 0; System.out.println(num1 / num2); }
6. Custom exception class must inherit an exception parent class:
public class ExceptionTest extends Exception{ @Override //这个是注解,表示这是重写的方法 public void printStackTrace() { System.out.println("自己定义的异常类"); System.out.println("尝试一下如果程序没有出现异常,强制抛出这个自定义异常,可不可以捕获"); } }
7. Try to use (catch) a custom exception class
public class Main { public static void main(String[] args) { try { test(); //调用这个方法尝试捕获自定义异常 } catch (ExceptionTest e) { e.printStackTrace(); //自定义的异常类重写了printStackTrace这个方法 } } //继续往上抛出异常 public static void test() throws ExceptionTest{ throw new ExceptionTest(); //强制产生一个自定义的异常并往上抛 } }
If there are any mistakes, please criticize and point them out! For more related questions, please visit the PHP Chinese website: JAVA Video Tutorial
The above is the detailed content of Detailed explanation of JAVA exception class structure (with examples). For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

Dreamweaver CS6
Visual web development tools