Home > Java > Javagetting Started > body text

In Java language, what is the difference between throw and throws

王林
Release: 2020-07-21 16:52:40
forward
5222 people have browsed it

In Java language, what is the difference between throw and throws

Difference analysis:

(Recommended tutorial: java introductory tutorial)

throws: Used to declare that a method may generate All exceptions will be uploaded without any processing, and will be thrown to whoever calls.

  • is used after the method declaration, followed by the exception class name

  • can be followed by multiple exception class names, separated by commas

  • indicates that an exception is thrown, which is handled by the caller of the method

  • throws indicates the possibility of an exception, which may not necessarily occur. These exceptions

throw: are used to throw a specific exception type.

  • Used in the method body, followed by the exception object name

  • Only one exception object name can be thrown

  • means throwing an exception, which is handled by the statements in the method body

  • throw means throwing an exception, and executing throw must throw some kind of exception

Let’s introduce them separately:

throws declares exceptions after the method. In fact, it means that you don’t want to do anything with the exception, tell others about the exceptions that may occur, and leave it to others. deal with.

Code example:

package com.xinkaipu.Exception;
class Math{
    public int div(int i,int j) throws Exception{
        int t=i/j;
        return t;
    }
}
public class ThrowsDemo {
    public static void main(String args[]) throws Exception{
        Math m=new Math();
   }
}
Copy after login

(Video tutorial recommendation: java video tutorial)

throw: handle an exception yourself, or catch the exception yourself The try...catch code block either throws an exception (throws exception).

Code implementation:

package com.xinkaipu.Exception;
 
public class TestThrow
{
    public static void main(String[] args) 
    {
        try
        {
            //调用带throws声明的方法,必须显式捕获该异常
            //否则,必须在main方法中再次声明抛出
            throwChecked(-3);            
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        //调用抛出Runtime异常的方法既可以显式捕获该异常,
        //也可不理会该异常
        throwRuntime(3);
    }
    public static void throwChecked(int a)throws Exception
    {
        if (a > 0)
        {
            //自行抛出Exception异常
            //该代码必须处于try块里,或处于带throws声明的方法中
            throw new Exception("a的值大于0,不符合要求");
        }
    }
    public static void throwRuntime(int a)
    {
        if (a > 0)
        {
            //自行抛出RuntimeException异常,既可以显式捕获该异常
            //也可完全不理会该异常,把该异常交给该方法调用者处理
            throw new RuntimeException("a的值大于0,不符合要求");
        }
    }
}
Copy after login

The above is the detailed content of In Java language, what is the difference between throw and throws. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template