- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to catch an assertion error in Java
In order to catch the assertion error, we need to declare the assertion statement in the try block with the second expression being the message to be displayed and catch the assertion error in the catch block.
Let us see a program on how to handle Assertion Error in Java −
Example
public class Example { public static void main(String[] args) throws Exception { try { assert args.length > 0 : "Assertion error caught!!!!"; } catch (AssertionError e) { System.out.println(e.getMessage()); } } }
Output
- Related Articles
- How to handle Assertion Error in Java?
- How to catch a divide by zero error in C++?
- How to read DataInputStream until the end without need to catch an EOFException in java?
- Can we have an empty catch block in Java?
- Multiple catch in Java
- Multi-catch in Java
- Is it possible to catch multiple Java exceptions in single catch block?
- Unable to locate an element using xpath error in selenium-java
- How to catch an exception thrown by an async void method in C#?
- How to raise an error within MySQL?
- How to throw an error in an async generator function in JavaScript ?
- How to use Assertion in response in Rest Assured?
- Can we to override a catch block in java?
- How to catch exceptions in JavaScript?
- What is the catch block in Java?

Advertisements