search
HomeJavaJavaBasejava custom error exception

java custom error exception

Nov 14, 2019 am 11:59 AM
javaCustom exception

java custom error exception

java custom error exception

The following code defines an exception to solve the problem of division. If the divisor appears 0 Or a negative number will report an error

Note that the custom exception code must be written in front of the application when it is run for the first time, otherwise an error will occur

无法加载主类
java.lang.NoClassDefFoundError

Copy the following program and run it on java

/*
 *自定义的异常类,格式如下,通过super关键字,将错误信息传递给Exception的构造函数中,之后再调用
 *toString()方法就可以打出自己想写的异常信息了
 */
class MyException extends Exception
{
    MyException(String ErrorMessage)
    {
        super(ErrorMessage);
    }
}
/*
 * 自定义异常:java自己的异常可以手动抛出也可以自动抛出,而自己定义的异常java虚拟机不认识
 * 所以,我们要通过throw关键字自己抛出异常,抛出异常之后我们有两种处理方式,第一种是抛出
 * 第二种是在下面直接try catch进行处理
 */
public class Myyichang 
{
    public static void main(String[] args) 
    {
            try
            {
                int c=chu(2,-1);
                System.out.println(c);
            }
            catch(MyException e)
            {
                System.out.println(e.toString());
            }
    }
    static int chu(int a,int b)throws MyException 
    {
        if(b<=0) 
        {
            throw new MyException("出现负数或者零了");
        }
        return a/b;
    }
}

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of java custom error exception. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment