Java runtime error что это

от admin

How to Solve the Most Common Runtime Errors in Java

A runtime error in Java is an application error that occurs during the execution of a program. A runtime error occurs when a program is syntactically correct but contains an issue that is only detected during program execution. These issues cannot be caught at compile-time by the Java compiler and are only detected by the Java Virtual Machine (JVM) when the application is running.

Runtime errors are a category of exception that contains several more specific error types. Some of the most common types of runtime errors are:

  • IO errors
  • Division by zero errors
  • Out of range errors
  • Undefined object errors

Runtime Errors vs Compile-Time Errors

Compile-time errors occur when there are syntactical issues present in application code, for example, missing semicolons or parentheses, misspelled keywords or usage of undeclared variables.

These syntax errors are detected by the Java compiler at compile-time and an error message is displayed on the screen. The compiler prevents the code from being executed until the error is fixed. Therefore, these errors must be addressed by debugging before the program can be successfully run.

On the other hand, runtime errors occur during program execution (the interpretation phase), after compilation has taken place. Any code that throws a runtime error is therefore syntactically correct.

Runtime Errors vs Logical Errors

A runtime error could potentially be a legitimate issue in code, for example, incorrectly formatted input data or lack of resources (e.g. insufficient memory or disk space). When a runtime error occurs in Java, the compiler specifies the lines of code where the error is encountered. This information can be used to trace back where the problem originated.

On the other hand, a logical error is always the symptom of a bug in application code leading to incorrect output e.g. subtracting two variables instead of adding them. In case of a logical error, the program operates incorrectly but does not terminate abnormally. Each statement may need to be checked to identify a logical error, which makes it generally harder to debug than a runtime error.

What Causes Runtime Errors in Java

The most common causes of runtime errors in Java are:

  • Dividing a number by zero.
  • Accessing an element in an array that is out of range.
  • Attempting to store an incompatible type value to a collection.
  • Passing an invalid argument to a method.
  • Attempting to convert an invalid string to a number.
  • Insufficient space in memory for thread data.

When any such errors are encountered, the Java compiler generates an error message and terminates the program abnormally. Runtime errors don’t need to be explicitly caught and handled in code. However, it may be useful to catch them and continue program execution.

To handle a runtime error, the code can be placed within a try-catch block and the error can be caught inside the catch block.

Runtime Error Examples

Division by zero error

Here is an example of a java.lang.ArithmeticException , a type of runtime exception, thrown due to division by zero:

In this example, an integer a is attempted to be divided by another integer b , whose value is zero, leading to a java.lang.ArithmeticException :

Accessing an out of range value in an array

Here is an example of a java.lang.ArrayIndexOutOfBoundsException thrown due to an attempt to access an element in an array that is out of bounds:

In this example, an array is initialized with 5 elements. An element at position 5 is later attempted to be accessed in the array, which does not exist, leading to a java.lang.ArrayIndexOutOfBoundsException runtime error:

How to Solve Runtime Errors

Runtime errors can be handled in Java using try-catch blocks with the following steps:

  • Surround the statements that can throw a runtime error in try-catch blocks.
  • Catch the error.
  • Depending on the requirements of the application, take necessary action. For example, log the exception with an appropriate message.

To illustrate this, the code in the earlier ArithmeticException example can be updated with the above steps:

Surrounding the code in try-catch blocks like the above allows the program to continue execution after the exception is encountered:

Runtime errors can be avoided where possible by paying attention to detail and making sure all statements in code are mathematically and logically correct.

Track, Analyze and Manage Errors With Rollbar

![Rollbar in action](https://rollbar.com/wp-content/uploads/2022/04/section-1-real-time-errors@2x-1-300×202.png)

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing errors easier than ever. Try it today.

«Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind.»

Java runtime error что это

Error is an illegal operation performed by the user which results in the abnormal working of the program. Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before compiling and executing.

The most common errors can be broadly classified as follows:

1. Run Time Error:

Run Time errors occur or we can say, are detected during the execution of the program. Sometimes these are discovered when the user enters an invalid data or data which is not relevant. Runtime errors occur when a program does not contain any syntax errors but asks the computer to do something that the computer is unable to reliably do. During compilation, the compiler has no technique to detect these kinds of errors. It is the JVM (Java Virtual Machine) that detects it while the program is running. To handle the error during the run time we can put our error code inside the try block and catch the error inside the catch block.

For example: if the user inputs a data of string format when the computer is expecting an integer, there will be a runtime error. Example 1: Runtime Error caused by dividing by zero

What are Runtime errors in Java | Explained

This article presents a detailed overview of runtime errors in java and in this regard, we need to understand the following concepts:

So, let’s get started!

What are Runtime Errors in Java?

The errors that occur at the time of program execution are referred as runtime errors. These types of errors can’t be detected at the compile time as there is nothing wrong with their syntax. So, we can say that the program that is syntactically correct still throws an error at the time of program execution is called a runtime error.

Types of Runtime Errors in Java

There are multiple types of runtime errors that we can face at the time of program execution. Some frequently encountered runtime errors are listed below:

  • Input-output errors
  • Infinite loop error
  • Division by zero errors
  • Logic Errors
  • Out of range errors
  • Undefined object error

Factors that Cause Runtime Errors

There are numerous factors that cause Runtime errors, among them the most commonly encountered causes are listed below:

  • Dividing any numeric value by zero produces runtime errors.
  • Accessing an array-out-of-bounds.
  • Passing invalid data e.g. passing a numeric value to the non-numeric field.
  • Passing invalid parameters/arguments to a method.
  • Multiple processes trying to access the same resource at the same time.
  • Trying to store an incompatible type value to a collection.
  • Insufficient space/memory error in threads (OutOfMemoryError)

Examples of Runtime Errors

Let’s understand the concept of runtime errors with the help of examples.

Example

In this example, we have an array of size three:

The array’s length is three and we knew that the array’s indexing starts from zero. So, specifying ary[3] means we are trying to access the fourth element of the array. Syntactically, nothing wrong with it so, we didn’t face any error at compile time. However, the JVM will throw the error at runtime:

From the above snippet, we observe that an error occurs at run time when we try to access the out-of-range index.

For the clarity of concept let’s consider another example:

Example

This time we have a string assigned with a “null” value and we will try to find the length of the string:

Following will be the output for the above code snippet:

When we run the program, we encounter a NullPointerException because the string is null.

So, how to handle such runtime errors? Does java provide a solution to deal with such runtime errors? Of course, Java does.

How to Handle Runtime Errors

In java, Runtime errors can be solved with the help of try-catch statements, and to do so, we have to put the code that can throw a runtime error in the try-catch statements.

Example

Let’s consider the below code snippet to understand how to solve runtime errors using try-catch statements in java:

Now we surrounded the code within the try-catch statements that can throw the error:

Now this time instead of throwing the error, JVM shows the message that we specified within the catch block.

Conclusion

In Java, the programs that are syntactically correct but still throw some errors at the time of program execution are known as runtime errors. These errors occur because of different reasons such as division by zero, accessing an array out of bounds, passing invalid data e.g. passing a numeric value to the non-numeric field, etc. These types of errors can be handled by surrounding the try-catch block around the code that can throw the runtime errors. This write-up explains different aspects of runtime errors for example what are runtime errors, their types, causes, and how to fix these errors in java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.

What are Runtime Errors in Java?

The errors that occur at the time of program execution are referred as runtime errors. These types of errors can’t be detected at the compile time as there is nothing wrong with their syntax. So Runtime errors occur when the program has successfully compiled without giving any errors and creating a “.class” file. However, the program does not execute properly. These errors are detected at runtime or at the time of execution of the program.

The Java compiler does not detect runtime errors because the Java compiler does not have any technique to catch these errors as it does not have all the runtime information available to it. Runtime errors are caught by Java Virtual Machine(JVM) when the program is running.

These runtime errors are called exceptions and they terminate the program abnormally, giving an error statement.

Exceptions are errors thrown at runtime. We can use exception handling techniques in Java to handle these runtime errors. In exception handling, the piece of code the programmer thinks can produce the error is put inside the try block and the programmer can catch the error inside the catch block.

Runtime Error VS Compile-Time Error

A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime. We can easily fix a compile-time error during the development of code. A compiler cannot identify a runtime error.

Detection

Compilers can easily detect compile-time errors during the development of code.A compiler cannot easily detect a runtime error. Thus, we need to identify it during the execution of code.

Читать:
Как добавить кнопки на форму с

Reference

A compile-time error generally refers to the errors that correspond to the semantics or syntax.A runtime error refers to the error which are not detected by the compiler but we encounter them during the code execution (during runtime).

impact

Compile-Time Errors are semantics or syntax errors, they prevent the code from running (compiling) as it detect some syntax errors. Wheares Runtime Errors prevent the code from complete execution.

Fixation

We can easily fix a compile-time error during the development of code.A compiler cannot identify a runtime error. But we can fix it after the execution of code and identification of the code in prior.

Types of Runtime Errors

There are multiple types of runtime errors that we can face at the time of program execution. Some frequently encountered runtime errors are listed below:

Data entry errors : Input-output errors

There are innumerable ways user input can corrupt an application.

On an HTML-based comment board, a user who is allowed to innocently submit an unencoded less than (<) or greater than sign (>) has the potential to completely ruin the ability of that webpage to render. Similarly, text-processing systems built to expect all input in ASCII format can prematurely terminate if they receive emojis or nonstandard character inputs.

More nefariously, a favorite attack vector of hackers is SQL injection, which hides a harmful executable database query inside an otherwise innocuous input field. This not only can cause an application to fail, but potentially surrender control of the entire data layer.

The process of input sanitization, or data scrubbing, converts the broad spectrum of data that could potentially be entered into applications into a safe range of values that a program comprehends. Use of such libraries helps mitigate runtime errors caused by input sanitization failures.

Popular Java frameworks such as Apache BVal and Hibernate Validator perform simple, annotation-based input cleansing, and they integrate easily into any Java-based application.

Poorly implemented logic

Just because code compiles doesn’t mean it works properly. Code often contains logical problems that cause an application to fail at runtime.

Java contains a built-in construct to handle a class of common code-related runtime errors, called the RuntimeException, or the unchecked exception. Java 17 defines 78 such errors in the SDK alone, and other projects and frameworks additionally define their own RuntimeException errors.

  • RuntimeException
  • ArrayIndexOutOfBoundException
  • ConcurrentModificationException
  • ClassCastException

Developers are not required to handle unchecked exceptions in their code. But an unchecked exception that is thrown and ignored will terminate an application.

At the very least, every application should include a generic exception harness that can catch every possible RuntimeException, log the error and allow the problematic thread of execution to die rather than abort the entire application.

Insufficient runtime resources

Software developers don’t shoulder the blame for every type of runtime error that occurs. Many runtime errors in Java involve resource limitations caused by problems with the underlying infrastructure. Examples include: network timeouts, out of memory conditions, CPU overutilization or an inability to schedule a thread on the processor.

One way to avoid resource-related runtime errors is to use a load testing tool, such as JMeter or LoadRunner, in an application’s CI/CD pipeline. If these tools detect a possible performance problem, they can stop the application before it moves further down the pipeline toward production deployment.

Some applications’ load varies drastically. For example, a financial services app may see steady load most of the time but be extremely busy at the end of trading day. A tax service might hit a peak load before the filing deadline but have relatively little load the rest of the year.

DevOps teams must monitor their performance metrics with tools to preemptively detect and mitigate resource-related runtime errors. Examples of such tools include JDK Flight Recorder and Java Mission Control.

For applications with completely unpredictable workloads, use cloud-based load balancing technology to allocate resources elastically. This eliminates both underallocated resources, and the trap of purchasing expensive, rarely used hardware.

External resource configuration

Enterprise applications rarely exist in an isolated bubble. They typically interact with everything from NoSQL databases and relational systems to Kafka queues and RESTful APIs. Unfortunately, if your application is unable to connect to a required, external system, this inevitably results in a runtime error.

An external resource can precipitate a runtime error if any of the following situations occur with no corresponding update to the calling program:

  • an IP address changes;
  • credentials change;
  • firewall configuration change; or
  • the external system goes down for maintenance.

Applications should react nimbly when resources change. The 12-Factor App insists developers keep all configuration data external to the application so that applications can react nimbly when resources change. The ability to update property files without changing the codebase allows applications to deal with external resource changes, and avoids an application rebuild.

Relatedly, chaos engineering tools randomly terminate the processes upon which an application depends. Such tools force developers to write code that remains responsive and resilient even when external systems fail.

The inherent problem with external resources is that developers cannot control them — but they can control how an application responds when those external resources fail. Anticipate runtime errors generated by the systems you don’t control, and write applications that respond gracefully when those external systems fail.

Third-party library vulnerabilities

Any nontrivial enterprise application includes dozens of dependencies on third-party libraries to perform functions such as logging, monitoring, input validation, form handling and more.

Unfortunately, any bugs in a third-party library become a bug in the application you deploy. This became uncomfortably real for the Java world in December 2021, as an LDAP inject flaw in the widely used Log4j 2 library forced JVMs throughout the world to go offline.

One way to mitigate against the possibility that software dependencies introduce runtime errors into applications is to only use trusted libraries from organizations such as Apache or Eclipse.

Another protective measure is to regularly update an application’s dependencies to the latest version as soon as updates become available.

Finally, be aware of secondary or tertiary dependencies — ones that your primary dependencies themselves rely upon — and be aware of any risks those bring with them.

In the world of software development, “perfect” is the enemy of “done.” No program is immune from the threat of an unanticipated runtime error. However, when enterprise developers raise their awareness of the possible causes and take steps to mitigate against potential threats, they can create software that minimizes the probability of encountering a runtime error.

How runtime error appears?

Congratulations! If your command prompt looks like this:

Then you have successfully compiled your program into Java bytecode, meaning that there were no syntactic issues with your code! But, as the filename implies, when we run this program:

We have now run into a runtime error. Run time errors generally occur when there is something logically incorrect with your code. This document will explain how to read and fix runtime errors.

Here are the major parts of the runtime error

Exception in thread “main”: This part appears for most runtime errors. It just tells you that the exception occurred in the main method, which should happen all the time.

java.lang.ArrayIndexOutOfBoundsException: 5: This is the error that occurred. The name of the error will generally give you an idea of what might have happened. In this case, we know it deals with arrays and going out of the allotted space for an array.

at RunTimeErrors.main(RunTimeErrors.java:10): This is called the stack trace, which tells you the list of the methods executing when the error occurred.

  • RunTimeErrors.main tells us that this error occurred within the main method of the RunTimeErrors class.
  • RunTimeErrors.java:10 tells us the file this occurred in and what line it happened at. In this case, this happened in our main class at line 10.

Do i need to throw Runtime Errors

Sometimes you need to perform a checking to decide wheather to throw a runtime exception if the system can’t do it alone, often when working with IllegalArgumentException, but most of the time you don’t need to perform a checking because this is part of the standard runtime checking that Java performs for you.

lets take NullPointerException as an example :

It can be a bit horrifying to think that you must check for null on every reference that is passed into a method (since you can’t know if the caller has passed you a valid reference). Fortunately, you don’t — this is part of the standard runtime checking that Java performs for you, and if any call is made to a null reference, Java will automatically throw a NullPointerException. So the above bit of code is always superfluous, although you may want to perform other checks in order to guard against the appearance of a NullPointerException.

What happend when you don’t catch a Runtime Exceptions

What happens when you don’t catch Runtime exceptions? Since the compiler doesn’t enforce exception specifications for these, it’s quite plausible that a RuntimeException could percolate all the way out to your main( ) method without being caught. To see what happens in this case, try the following example:

You can already see that a RuntimeException (or anything inherited from it) is a special case, since the compiler doesn’t require an exception specification for these types. The output is reported to System.err:

So the answer is: If a RuntimeException gets all the way out to main( ) without being caught, printStackTrace( ) is called for that exception as the program exits.

Keep in mind that only exceptions of type RuntimeException (and subclasses) can be ignored in your coding, since the compiler carefully enforces the handling of all checked exceptions. The reasoning is that a RuntimeException represents a programming error, which is:

An error you cannot anticipate. For example, a null reference that is outside of your control.

An error that you, as a programmer, should have checked for in your code (such as ArraylndexOutOfBoundsException where you should have paid attention to the size of the array). An exception that happens from point #1 often becomes an issue for point

You can see what a tremendous benefit it is to have exceptions in this case, since they help in the debugging process.

It’s interesting to notice that you cannot classify Java exception handling as a single-purpose tool. Yes, it is designed to handle those pesky runtime errors that will occur because of forces outside your code’s control, but it’s also essential for certain types of programming bugs that the compiler cannot detect.

Runtime Error Example

When we run the above program, it throws the following NullPointerException error message.

We are getting NullPointerException in the statement t.foo(“Hi”); because “t” is null here.

There’s a whole group of exception types that are in this category. They’re always thrown automatically by Java and you don’t need to include them in your exception specifications. Conveniently enough, they’re all grouped together by putting them under a single base class called RuntimeException, which is a perfect example of inheritance: It establishes a family of types that have some characteristics and behaviors in common. Also, you never need to write an exception specification saying that a method might throw a RuntimeException (or any type inherited from RuntimeException), because they are unchecked exceptions. Because they indicate bugs, you don’t usually catch a RuntimeException — it’s dealt with automatically. If you were forced to check for RuntimeExceptions, your code could get too messy. Even though you don’t typically catch RuntimeExceptions, in your own packages you might choose to throw some of the RuntimeExceptions.

Похожие статьи