小程序自定义单个页面标题栏
As we know that Java provides an efficient mechanism for handling the exception but still user has the option to define its own exception in Java, using try-catch blocks or using throw keyword. List of various user-defined exception keywords provided in Java is as follows:
众所周知,Java提供了一种有效的机制来处理异常,但是用户仍然可以选择使用try-catch块或throw关键字在Java中定义自己的异常。 Java提供的各种用户定义的异常关键字的列表如下:
Try
尝试
Catch
抓住
Throw
扔
Throws
投掷
Finally
最后
Let us write a simple java code which includes all these keywords
让我们编写一个包含所有这些关键字的简单Java代码
import java.io.IOException;
import java.util.Scanner;
public class Main
{
static void ex(int a)
{
try
{
int c =50/0;
}
catch(ArithmeticException e)
{
System.out.println("dividing a number by zero cannot be defined");
}
finally
{
System.out.println("this statement will always be executed");
}
if(a>=18)
throw new ArithmeticException("The person is eligible to vote");
else
System.out.println("the person is not eligible to vote");
}
public static void main(String [] args) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the age of a person to check for voting");
int c =sc.nextInt();
ex(c);
System.out.println("This is written inside main function");
}
}
Output
输出量
First run:
enter the age of a person to check for voting
12
dividing a number by zero cannot be defined
this statement will always be executed
the person is not eligible to vote
This is written inside main function
Second run:
enter the age of a person to check for voting
34
dividing a number by zero cannot be defined
this statement will always be executed
Exception in thread "main" java.lang.ArithmeticException: The person is eligible to vote
at Main.ex(Main.java:22)
at Main.main(Main.java:32)
小程序自定义单个页面标题栏