search
HomeJavajavaTutorialCan exception classes be customized in Java?

Can exception classes be customized in Java?

Jul 17, 2020 pm 04:37 PM
javaException class

Can exception classes be customized in Java?

First of all, exception classes can be customized in Java. So how to customize exception classes in Java?

(Recommended tutorial: java introductory tutorial)

1. First write a class, inherit Exception, or inherit RuntimeException;

2. Provide two There are two constructors, one without parameters and one with String parameters. Just write super(s) in the method body with parameters.

(Video tutorial recommendation: java video tutorial)

Code implementation:

public class Test13 {
    public static void main(String[] args) {
        // 创建自定义异常类的实例,并不抛出
        MyStackOperatorException e = new MyStackOperatorException("栈操作有误");
        String msg = e.getMessage();
        System.out.println(msg);  // 栈操作有误
        e.printStackTrace();  // exception.MyStackOperatorException: 栈操作有误 
        //at exception.Test13.main(Test13.java:23)
}

// 自定义异常类
class MyStackOperatorException extends Exception {
    public MyStackOperatorException() {
    }
    public MyStackOperatorException(String s) {
        super(s);
    }
}

The above is the detailed content of Can exception classes be customized in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools