Exception handling | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Exception handling

Can we handle any exception without using catch block?..if yes then how?

13th Dec 2019, 10:41 AM
Giriraj Nagar
Giriraj Nagar - avatar
2 Answers
+ 2
You can assign the exception right after the function name using the throws keyword Scanner sc = new Scanner(System.in); int getInput() throws IOException { int x = Integer.parseInt(sc.nextLine()) ; return x; }
13th Dec 2019, 11:16 AM
HNNX 🐿 - avatar
+ 2
Just like HNNX 🐿 mentioned you can either use a "throws" keyword with the method declaration or you need to handle it with a try catch block inside the method.
13th Dec 2019, 6:47 PM
Avinesh - avatar