编译时间和运行时错误有什么区别

编译时间和运行时错误有什么区别

本文介绍了编译时间和运行时错误有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提供一个解释编译时错误和运行时错误之间差异的示例吗?

Can anyone provide an example which explains the difference between a compile time error and a runtime error?

推荐答案

int i = 17;
if (i - 5)
   {
   }

会导致编译错误,因为"i-5"不是布尔值-它是整数-并且if条件必须在C#中为bool

运行时错误是仅在程序运行时才检测到的错误:

will cause a compilation error, because "i - 5" is not a boolean vlaue - it is integer - and if conditions must be bool in C#

A run time error is one that is only detected when the program runs:

int i = 17;
int[] ar = new int[3];
ar[i] = 42;

将导致运行时错误,因为用作数组索引的i的值超出了数组的范围.

运行时错误可以用try...catch块捕获,而编译时错误则不能.

will cause a run time error, because the value of i being used as an array index is outside the bounds of the array.

Run time errors can be trapped with a try...catch block, compile time errors cannot.




这篇关于编译时间和运行时错误有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:52