'How to print something only if an Exception is not raised? [duplicate]
I am new to java but quite familiar with python. I was learning about try-catch blocks. I need to print something if there are no exceptions.
To do that in python we can use else with try - except block. I need to know what is the method that can be used in java to do this task.
Complete python code.
try:
n = int(input())
except Exception as e:
print(e)
else:
print("No exception is raised")
Incomplete java code to do the same thing.
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
}
catch(Exception e) {
System.out.println(e);
}
// need to add something
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
