小程序自定义单个页面标题栏_在单个Java程序中使用所有用户定义的异常关键字...

小程序自定义单个页面标题栏

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提供的各种用户定义的异常关键字的列表如下:

  1. Try

    尝试

  2. Catch

    抓住

  3. Throw

  4. Throws

    投掷

  5. 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)


翻译自: https://www.includehelp.com/java-programs/use-of-all-the-user-defined-exception-keywords-in-a-single-java-program.aspx

小程序自定义单个页面标题栏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值