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

Fundamentals of Scala Programming

Mastering the Fundamentals of Scala Programming: A Complete Guide to Functional Programming

Introduction to Scala Programming Scala programming has gained immense popularity in recent years thanks to...
Read More
Java Programming

Java Basics for Beginners: A Step-by-Step Guide to Mastering Java Programming

Introduction to Java programming Java, a versatile and extensively utilised programming language, is popular among...
Read More
Entity Relationship Diagrams (ERD)

Entity Relationship Diagrams (ERD): A Comprehensive Guide

Introduction to Entity Relationship Diagrams (ERD) Entity Relationship Diagrams (ERD) are powerful tools used in...
Read More