How do i call a function when an exception occurs in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How do i call a function when an exception occurs in java

Suppose i want to call a function handleError when i get any exception in code then how do I add it

26th Nov 2018, 8:19 PM
Madhusudan Babar
Madhusudan Babar - avatar
7 Answers
+ 6
Example: try{ int a= 5/0; System.out.print(a); } catch (ArithmeticException e){ System.out.print("Arithmetic exception occurs"); } catch (Exception e){ System.out.print("Error"); } //You can include catch statements as many as you want. // A try statement can only execute ONE exception message https://www.sololearn.com/learn/Java/2175/?ref=app
27th Nov 2018, 1:28 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 2
Hmmm you are trying to catch exceptions to build your own errors messages. Firstly you need to explain in what language you wish to do that, the synthax is different following the language.
26th Nov 2018, 8:37 PM
Geoffrey L
Geoffrey L - avatar
+ 2
In java
26th Nov 2018, 8:38 PM
Madhusudan Babar
Madhusudan Babar - avatar
+ 2
Oops, sorry I don't know java. I don't want to missguide you.
26th Nov 2018, 8:44 PM
Geoffrey L
Geoffrey L - avatar
+ 2
OK thanks
26th Nov 2018, 8:45 PM
Madhusudan Babar
Madhusudan Babar - avatar
+ 2
It would be something like this: try{ //Your Code ... } catch( Exception e ) { handleError(); }
26th Nov 2018, 11:11 PM
bullion
+ 1
try...catch
28th Nov 2018, 1:11 AM
Alvin Milton
Alvin Milton - avatar