Can try be without catch in java

Can try be without catch in java

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java
Читайте также:  Создать массив кортежей python

Источник

Can we have try without catch block in java

You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit().

Lets understand with the help of example.

When you execute above program, you will get following output:

What happens when you have return statement in try block:

If you have return statement in try block, still finally block executes.

When you execute above program, you will get following output:

What happens if you have return statement in finally block too

It overrides whatever is returned by try block. Lets understand with the help of example:

When you execute above program, you will get following output:

What if exception is thrown in try block

If exception is thrown in try block, still finally block executes.

When you execute above program, you will get following output:

As you can see that even if code threw NullPointerException, still finally block got executed.
You can go through top 50 core java interview questions for more such questions.

Was this post helpful?

Share this

Author

Java try with resources

Table of ContentsOlder approach to close the resourcesJava 7 try with resourcesSyntaxExampleJava 9 Try with resources ImprovementsFinally block with try with resourcesCreate Custom AutoCloseable Code In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. It helps to […]

Exception Handling Interview Questions

Java Exception Handling Interview Questions And Answers

Table of ContentsQuestion 1: What is Exception ?Question 2: How can you handle exception in java?Question 4: Difference between checked exception, unchecked exceptionand and errorsQuestion 5: Can we have try without catch block in java ?Question 6: What is RunTime exception in java?Question 7: What is checked exception or compile time exception?Question 8: Can you put […]

Difference between throw and throws in java

Table of Contentsthrow:throws: In this tutorial, we are going to see difference between throw and throws in java. throw: throw keyword is used to throw any custom exception or predefine exception. For example: Let’s say you want to throw invalidAgeException when employee age is less than 18. Create a Employee class as below. [crayon-64b704305e1dc704350077/] Create […]

Exception handling in java

Table of ContentsExceptionsWhat is Exception ?Exceptions hierarchy ThrowableExceptionRuntimeExceptionErrorChecked ExceptionsOCAJP checked exceptionsUnchecked ExceptionsOCAJP unchecked ExceptionsArrayIndexOutOfBoundsException ClassCastExceptionIllegalArgumentException NullPointerExceptionNumberFormatExceptionErrors ExceptionInInitializerErrorStackOverflowErrorNoClassDefFoundErrorDifference between checked exception, unchecked exception and errorsConclusionReferences Exceptions I have started writing about the and how to prepare for the various topics related to OCAJP exams in my blog. In my previous post, I have published few sample mock questions for StringBuilder […]

How to create custom exception in java

In this post, we will see how to create custom exception in java. It is very simple to create custom exception in java. You just need to extends Exception class to create custom exception. Lets understand this with example. You have list of counties and if You have “USA” in list of country, then you […]

Difference between checked and unchecked exception in java

Table of ContentsWhat is Exception?What is checked exception?What is unchecked exception? In this post, we will see difference between checked and unchecked exception in java. It is important question regarding exceptional handling. What is Exception? Exception is unwanted situation or condition while execution of the program. If you do not handle exception correctly, it may […]

Источник

How to use try without catch in Java

We can use try without a catch or finally block in Java. But, you have to use a finally block.

The finally block always executes when the try block exits. finally block is executed even if an exception occurs. finally block is used for cleanup code. For example, if you open a file in the try block, you can close it inside finally.

If JVM exits, this block may not execute.

Example of try without catch block:

Let’s try it with an example:

class Main  public static void main(String[] args)  try  System.out.println("Inside try block.."); > finally  System.out.println("Inside finally.."); > > >

This program will work. If you run this program, it will print the below output:

Inside try block.. Inside finally..

Exception inside try block:

Let’s try to throw an exception inside the try block.

class Main  public static void main(String[] args)  try  throw new NullPointerException(); > finally  System.out.println("Inside finally.."); > > >

We are throwing a NullPointerException inside the try block. It will print:

Inside finally.. Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:4) Process finished with exit code 1

Java exception try finally

Without catch block and with throws:

Let’s take a look at the below program:

class Main  private static void dummyMethod() throws NullPointerException  try  System.out.println("Inside dummyMethod try. "); throw new NullPointerException(); > finally  System.out.println("Inside finally.."); > > public static void main(String[] args)  try  System.out.println("Inside main try. "); dummyMethod(); > finally  System.out.println("Inside main finally. "); > > >

In this example, I am calling a method dummyMethod that can throw a NullPointerException. Inside this method, we are throsing NullPointerException in the try block.

Both uses only try with only finally block and without catch block.

If you run this program, it will print the below output:

Inside main try... Inside dummyMethod try... Inside finally.. Inside main finally... Exception in thread "main" java.lang.NullPointerException at Main.dummyMethod(Main.java:6) at Main.main(Main.java:15)

As you can see, it prints the statements in both finally blocks.

Источник

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