Java System exit() 方法

2024 年 11 月 7 日 | 阅读 1 分钟

System 类的 exit() 方法会终止当前正在运行的 Java 虚拟机。该方法接受一个状态码作为参数。

  • 状态码 - exit(0) - 表示成功终止
  • 状态码 - exit(-1) - 表示异常终止
  • 状态码 - exit(1) - 表示不成功终止

语法

参数

status - 它是退出状态码。

返回值

此方法不返回任何值。

Exception

如果存在安全管理器并且其 checkExit 方法不允许以指定状态码退出,则会抛出 SecurityException

示例 1

输出

array[0]=9
array[1]=8
array[2]=7
array[3]=6
terminatingjvm,exiting

示例 2

输出

program will terminate when i is 1
your no is 10
your no is 9
your no is 8
your no is 7
your no is 6
your no is 5
your no is 4
your no is 3
your no is 2
your no is 1
Value is 1 now terminating your program