UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

I want to rethrow an exception, is that possible?

Asked by Last Modified  

12 Answers

Learn Java

Follow 1
Answer

Please enter your answer

Professional Tutor with 15 years of experience.

yes we can
Comments

Tutor

yes.We can rethrow an exception.try { // code here that throws an Exception } catch (Throwable t) { throw t; // (re)throw it in their face } Example: //Rethrowing.java // Demonstrating fillInStackTrace() public class Rethrowing { public static void first() throws Exception { System.out.println("originating...
read more
yes.We can rethrow an exception.try { // code here that throws an Exception } catch (Throwable t) { throw t; // (re)throw it in their face } Example: //Rethrowing.java // Demonstrating fillInStackTrace() public class Rethrowing { public static void first() throws Exception { System.out.println("originating the exception in first()"); throw new Exception("thrown from first()"); } public static void getexception() throws Throwable { try { first(); } catch(Exception e) { System.out.println( "Inside getexception (), e.printStackTrace()"); e.printStackTrace(); throw e; // 17 // throw e.fillInStackTrace(); // 18 } } public static void main(String[] args) throws Throwable { try { getexception (); } catch(Exception e) { System.out.println( "Caught in main, e.printStackTrace()"); e.printStackTrace(); } } } ///:~ The important line numbers are marked inside of comments. With line 17 un-commented (as shown), the output is: originating the exception in f() Inside getexception (), e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.first(Rethrowing.java:8) at Rethrowing. getexception (Rethrowing.java:12) at Rethrowing.main(Rethrowing.java:24) Caught in main, e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.first(Rethrowing.java:8) at Rethrowing. getexception (Rethrowing.java:12) at Rethrowing.main(Rethrowing.java:24) So the exception stack trace always remembers its true point of origin, no matter how many times it gets rethrown. With line 17 commented and line 18 un-commented, fillInStackTrace( ) is used instead, and the result is: originating the exception in f() Inside getexception (), e.printStackTrace() java.lang.Exception: thrown from first() at Rethrowing.first(Rethrowing.java:8) • at Rethrowing. getexception (Rethrowing.java:12) • at Rethrowing.main(Rethrowing.java:24) read less
Comments

A software veteran with more than decade of experience

yes...you can use catch(Exception ex) { {some logic} throw ex; }
Comments

Software Development Trainer

What do you mean by rethrow? Are you trying to ask if one can raise the exception again once it's handled in catch? If yes, then yeah we can. We need to use throw keyword. Something like this try{ //code } catch (exception ex) { //log the exception or do whatever you want. throw...
read more
What do you mean by rethrow? Are you trying to ask if one can raise the exception again once it's handled in catch? If yes, then yeah we can. We need to use throw keyword. Something like this try{ //code } catch (exception ex) { //log the exception or do whatever you want. throw ex;//rethrow exception } read less
Comments

Trainer

If you develop your own library which makes use of a third-party library that throws an exception. Now there are two options for us: 1. don't catch it (not a better option) 2. catch it and log it somewhere, then rethrow it By rethrowing, we propgate this exception to the caller (client) of your...
read more
If you develop your own library which makes use of a third-party library that throws an exception. Now there are two options for us: 1. don't catch it (not a better option) 2. catch it and log it somewhere, then rethrow it By rethrowing, we propgate this exception to the caller (client) of your library and request it to catch the excpetion. This way your delegating the responsibility of handling this excpetion to the caller of our library read less
Comments

Java Web Application Development and Aptitude Trainer

yes you can.
Comments

Java Tutor

yes...you can using catch block with appropriate Exception argument
Comments

Talent with Professional Experience

Yes you can rethrow an exception by throwing an exception inside catch block provided we are able to catch the Exception properly. If we are not sure what kind of Exception is caught; in that case we can catch the super Excepiton class incatch block and then throw any other exception
Comments

Java Technical Leader

try { } catch(Exception ex) { }
Comments

Java Technical Leader

try { // code here } catch(Exception ex) { throw ex; } but this is not advisable hust catch the same exception and re-throw it. If you want to make your application specific message then you can try this.
Comments

View 10 more Answers

Related Questions

Why Java doesnot have Pointer ?
The reasons are Pointers are fundamentally unsafe. Java has a robust security model and disallows pointer arithmetic.Java ensures pointer access via indexed array. A big advantage of Java's indexed array...
Satyajit
Why does â??5/2 = 2â?? and not 2.5 in Python?
Due to dynamic Typing. use Numpy , and declare data type , you will get desired result
Srikanth
0 0
6
How can I learn Java from home?
Hi Divya, There are several ways to learn Java from home. 1. You can find trainer on UrbanPro. 2. You can find free resources available online like youtube or javaranch, stackoverflow 3.You can connect...
Divya
0 0
5
how to run a java program
Two ways you can run a Java Program. 1- Through cmd prompt, 2- Through Eclipse. 1- cmd prompt (Write your program and save the file with .java extension. Now open your cmd prompt and give the java file...
Prem Kumar Pathak

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Facebook Analytics
Assume how the Facebook application will store the millions of customer's record in real-time: facebook = { 'jose': { 'name': 'jose', 'age': 33, 'hobby': , # cricket,football 'mobile': 1111111111, 'email':...

How do i get best Campus / Off Campus Placement?
Companies are looking for Skilled Freshers. So build your technical skills while doing MCA / BTech / BCA / BSc (IT or CS) into below areas- 1. Strong your programming & debugging skills ...

JAVA Online Training
What is Java? Java is a technology developed by James Gosling at Sun Microsystems, Sun Microsystems is now a part of Oracle Corporation. This is one of the most powerful & securable language used to...

What Are IT Industries Performance Metrics?
1. Outstanding Expectation: Eligible to get Promotion easily and good salary hike. Always preferrable to go abroad. 2. Exceed Expectation: Can get Promotion as per schedule of company with good salary...

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 0
0

Recommended Articles

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

Java is the most commonly used popular programming language for the creation of web applications and platform today. Integrated Cloud Applications and Platform Services Oracle says, “Java developers worldwide has over 9 million and runs approximately 3 billion mobile phones”.  Right from its first implication as java 1.0...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more