Assignments/Article By Jakub Lisowski

Compile-Time Exceptions and Runtime Exceptions in Java

Student Assignment

Compile-Time Exceptions and Runtime Exceptions in Java

Published on June 16, 2022

Compile-Time Exceptions

Compile-time errors are errors that happen when we use the incorrect syntax or semantics of any programming language. The compiler will then throw compile-time errors at us. Until all of the program’s errors are fixed, the compiler won’t allow it to execute.

Characteristics of Compile-Time Exceptions:

  • Early identification of potential issues: By detecting exceptions at compile-time, developers can address them before executing the code
  • Compiler ensures handling or declaration: The Java compiler enforces that all checked exceptions are either caught and handled or declared to be thrown, reducing the likelihood of unhandled exceptions slipping through

Example of Compile-time error

  1. int main()
  2. {
  3.     int a=20;
  4.     printf(“The value of a is : %d”,a):
  5.     return 0;
  6. }

In the above code, we have tried to print the value of ‘a’, but it throws an error. We put the colon at the end of the statement instead of a semicolon, so this code generates a compile-time error.

Runtime Exceptions

The Errors that happen during execution and after compilation are known as runtime errors. Runtime mistakes include, but are not limited to, division by zero. Since the compiler does not flag these problems, they are difficult to find.

Characteristics of Runtime Exceptions:

  • Not checked by the compiler: Unlike compile-time exceptions, the Java compiler does not verify the handling or declaration of runtime exceptions, providing more flexibility to the programmer.
  • Caused by logical errors or unexpected conditions: Runtime exceptions are usually a result of issues such as invalid input, improper usage of APIs, or unexpected program states.

Example of runtime error

  1. int main() 
  2.     int a=20; 
  3.     int b=a/0; // division by zero 
  4.     printf(“The value of b is : %d”,b): 
  5.     return 0; 

In the above code, we try to divide the value of ‘b’ by zero, and this throws a runtime error.

Summary

Compile-time and Runtime are the two programming terms used in software development. Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running. Both the compile-time and runtime refer to different types of error.

Student Assignment

The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites

Our Latest Blog

Build Strong Front-End Skills with the LSET Angular Developer Exam

Build Strong Front-End Skills with the LSET Angular Developer Exam

In today’s fast-paced digital world, front-end development is a crucial skill for building dynamic and...
Read More
Get Expert Help Anytime with HyperLearn from LSET

Get Expert Help Anytime with HyperLearn from LSET

Learning to code or solving technical challenges doesn’t have to be a lonely and frustrating...
Read More
Transform Your Coding Journey with HyperLearn by LSET

Transform Your Coding Journey with HyperLearn by LSET

In today’s fast-paced tech landscape, staying ahead in coding and software development is essential for...
Read More