Getting error code in java

Errors and Exceptions in Java

announcement - icon

As always, the writeup is super practical and based on a simple application that can work with documents with a mix of encrypted and unencrypted fields.

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Читайте также:  Найти путь до файла python

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll learn about Java errors and exceptions and their differences.

2. The Throwable Class

Error and Exception are both subclasses of the Throwable class and are used to indicate that an abnormal situation has happened. Furthermore, only instances of Throwable and its subclasses can be thrown by the Java Virtual Machine or caught in a catch clause.

Instances of Error and Exception are created to include information about the situation (for example, the stack trace):

Throwable

3. Error

Errors indicate abnormal situations that should never happen. An error is thrown when a serious problem has occurred. Further, errors are regarded as unchecked exceptions, and applications should not try to catch and handle them. Moreover, errors happen at run time and cannot be recovered.

public class ErrorExample < public static void main(String[] args) < throw new AssertionError(); >>

If we run the above code, we get the following:

Exception in thread "main" java.lang.AssertionError: at com.baeldung.exception.exceptions_vs_errors.ErrorExample.main(ErrorExample.java:6)

The code caused an error called the AssertionError, which is thrown to indicate when an assertion has failed.

Читайте также:  Java windows programming eclipse

4. Exception

Exceptions are abnormal conditions that applications might want to catch and handle. Exceptions can be recovered using a try-catch block and can happen at both run time and compile time.

Some of the techniques used for exception handling are try-catch block, throws keyword, and try-with-resources block.

Exceptions are divided into two categories: Runtime Exceptions and Checked Exceptions.

4.1. Runtime Exceptions

RuntimeException and its subclasses are the exceptions that can be thrown while the Java Virtual Machine is running. Further, they are unchecked exceptions. Unchecked exceptions don’t need to be declared in the method signature using the throws keyword if they can be thrown once the method is executed and propagate outside the method’s scope.

public class RuntimeExceptionExample < public static void main(String[] args) < int[] arr = new int[20]; arr[20] = 20; System.out.println(arr[20]); >>

After we run the above code, we get the following:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at com.baeldung.exception.exceptions_vs_errors.RuntimeExceptionExample.main(RuntimeExceptionExample.java:7)

As we can see, we got an ArrayIndexOutOfBoundsException which is a subclass of IndexOutOfBoundsException, which is itself a subclass of RuntimeException.

4.2. Checked Exceptions

Other exceptions that are not subclasses of RuntimeException are checked exceptions. They need to be declared in the method signature using the throws keyword if they can be thrown once the method is executed and propagate outside the method’s scope:

public class CheckedExceptionExcample < public static void main(String[] args) < try (FileInputStream fis = new FileInputStream(new File("test.txt"))) < fis.read(); >catch (IOException e) < e.printStackTrace(); >> >

If we run the above code, we get the following:

java.io.FileNotFoundException: test.txt (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at com.baeldung.exception.exceptions_vs_errors.CheckedExceptionExcample.main(CheckedExceptionExcample.java:9)

We got a FileNotFoundException which is a subclass of IOException, which is a subclass of Exception.

5. Conclusion

In this article, we learned the differences between errors and exceptions in the Java ecosystem.

Читайте также:  Css grid repeat auto fit

As always, the complete code samples are available over on GitHub.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью