2

Launch custom exceptions in Java / Android

 2 years ago
source link: https://www.codesd.com/item/launch-custom-exceptions-in-java-android.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Launch custom exceptions in Java / Android

advertisements

I'm developing a custom Exception class, in which I have the following constructors:

public class UserException extends Exception
{
    string message;
    public UserException() {
        super();
    }

    public UserException(String message, Throwable cause)
    {
        super(message, cause);

        this.cause = cause;
        this.message = message;
    }
}

And I create a new custom exception like this:

private Camera.PreviewCallback SetPreviewCallBack() throws UserException {
    .......
    // Something went wrong
    throw new UserException("Something failed.", new Throwable(String.valueOf(UserExceptionType.CaptureFailed)));
 }

But when I insert my throw new UserException(...) it tells me to surround it with try/catch!! That's not the idea, isn't it? I want to throw custom exceptions when I need them to be thrown, without surronding my new Exceptions with more try/catch clauses.

So, what I'm doing wrong? What I'm misunderstanding?


In addition to Eran's answer, you could also make your custom Exception extend RuntimeException, which does not need to be caught.

Related Articles

How to create custom exceptions in Java?

How do we create custom exceptions in Java?To define a checked exception you create a subclass (or hierarchy of subclasses) of java.lang.Exception. For example: public class FooException extends Exception { public FooException() { super(); } public F

Custom exception in java: 'can not find the symbol' if it is not explicitly imported

I am trying to catch an Exception in java, however when catching the Exception I get a cannot find symbol error. The error is at catch (ItemNotFoundException infe) and the compiler complains that ItemNotFoundException is no known class (symbol). Do n

How to write custom exceptions for java connection

I am building a web application i ve a login jsp form which will be redirected to a servlet... My servlet code: Login login=new Login(); login.setUserName(request.getParameter("txtUsername")); login.setPassWord(request.getParameter("txtPass

Launch custom exceptions, if for the loop

public class StringArray { private String strArr[]; public StringArray(int capacity) { strArr = new String [capacity]; } public int indexOf(String s) throws StringNotFoundException { for(int i=0;i<strArr.length ;++i) { if (strArr[i].equals(s)) { retu

Clarification on how to launch and capture custom exceptions in Java

So I have this program that is basically recreating SQL in java. I have this "driver" class that sorta controls the whole thing. It calls and instantiates various classes and methods to make it all work. With in this I have this method public vo

How to define my own message in my Custom Exception in java that can be reteived my getMessage () BUT WITHOUT using the constructor, is it possible?

I'm just Learning Exception Handling in Java, What i would like to know is rather than trying something like say, throw new Exception("My Message"); and String message=ex.getMessage(); System.out.println(message); Take a look at the code below ,

Saving and retrieving a list of custom objects in Java / Android

I am new to the world of Generics and am trying to write a utility class that will take a list of Objects and persist it to the store and then retrieve it back. This is what I wrote to save the list: public static void saveListToStore(Context ctx, St

Launch an exception in Java

I have a problem when I ask the user for the amount to be withdrawn from their balance. I have a method called withdraw, and i pass their balance. Then I want to check if the amount that they want to withdraw is less than their balance. If yes, I wou

The best way to write a custom exception name in Java

I just wonder about best way to write custom exception name, like if I have user and i want make exception for add and delete and update, what is better using names like : UserAddException UserUpdateException UserRemoveException or UserDeleteExceptio

Custom Exception-Java

Well, i'm programming a rent car system with clients and these have an id card. At the time of renting the car the client needs to identify himself with the id, so I need a custom exception that handles if the input of the user is 8 numbers and one l

Kotlin - Launch a custom exception

How can I throw a custom exception in Kotlin? I didn't really get that much off the docs... In the docs, it gets described what each exception needs, but how exactly do I implement it?One thing to keep in mind. If you have worked in Java. Kotlin is j

Java setting custom exceptions inside the class, is this bad?

I have a class that has a method. The method may fail and I want to throw an exception. Is it bad to define the exception class within the namespace of the class whose function is throwing the exception? I haven't seen a lot of examples of this, but

C # - Is it better to build a logic around launching a custom exception or business management?

If I want to build my business logic, is it a better practice to handle the logic by throwing custom exceptions or by adding specific "If" conditions to avoid a specific case? For instance (stupid, but simple example to get the point): var succe

No launcher activity found in eclipse java android

This question already has an answer here: What does it mean "No Launcher activity found!" 17 answers I'm getting something that looks like this: "No Launcher activity found! The launch will only sync the application package on the device!&q

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK